How to input text into text field with javascript

javascript input text
how to get value of textbox in javascript
how to get value of textbox in javascript from html
input value javascript
javascript set input value by name
how to take input in javascript from user
how to take number input from user in javascript
javascript default value for input field

I have a form on a wordpress site with a text field for an email adress which looks like this:

<div class="tnp-field tnp-field-email"><label>Email</label><input class="tnp-email" type="email" name="ne" required></div>

I can get the email address from a get parameter (www.domain.com/?email=test@mail.com) like this:

<script type='text/javascript'>
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

function getUrlParam(parameter, defaultvalue){
    var urlparameter = defaultvalue;
    if(window.location.href.indexOf(parameter) > -1){
        urlparameter = getUrlVars()[parameter];
        }
    return urlparameter;
}

var emailtxt = getUrlParam('email','');

How can I fill the text field with emailtxt?

You can just set the value of the input field (HTMLInputElement.value = "value").

document.querySelector("input[name=ne]").value = getUrlParam('email','');

<div class="tnp-field tnp-field-email"><label>Email</label><input class="tnp-email" type="email" name="ne" required></div>
<script type='text/javascript'>
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

function getUrlParam(parameter, defaultvalue){
    var urlparameter = defaultvalue;
    if(window.location.href.indexOf(parameter) > -1){
        urlparameter = getUrlVars()[parameter];
    }
    return urlparameter;
}

var emailtxt = getUrlParam('email','');
 console.log(emailtxt);
document.querySelector('input[name=ne]').value = emailtxt;
</script>

javascript to input text into a textbox, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Text fields (and, in fact, all input fields) have a value property. You can read this value as an ordinary string variable. You can also write to this property, and the text field will be updated on the fly. This code handles the data input and output: Create a variable for the name. This is an ordinary string variable. Copy the value of the textbox into the variable.

You can try DOM id selector:

<input class="tnp-email" type="email" name="ne" id="yourId" required>

document.getElementById("yourId").innerHTML = "Your text";

HTML DOM Input Text Object, The value property sets or returns the value of the value attribute of a text field. The value property contains the default value OR the value a user types in (or a  This page shows you to how to add (append) or replace text in a text area or text field. In the example below, the new text is entered in the left text area and added to the one on the right. We will also look at other options for acquiring the new text. Enter your text here, then click the button.

You can use DOM class name selector like this:

 var x = document.getElementsByClassName("tnp-email");
 x[0].value = "Your value";

HTML DOM Input Text value Property, Also we can use jquery val() method inside script to get or set the value of text input field. Using text value property: Syntax: Get value : textObject.value Set value  Add a string of text into an input field when user clicks a button. Basically just trying to add text to an input field that already contains a value.. the trigger being a button.. Before we click button, form field would look like.. After clicking button, field would look like.. Trying to accomplish using javascript only..

How to get the value of text input field using JavaScript , How is it possible to put text into a textbox using JavaScript? -magicyte. To extract the information which a user enters into the text fields using Javascript, the Javascript code to do would be: The line document.getElementById("firstname").value accesses the text box with an id of firstname and retrieves the value which the user has entered into this text box. Now that the text box value is accessed and retrieved, it must be stored. This is done by creating a Javascript variable and storing the value of the text box into this variable.

Adding Text Into Textbox with JavaScript, You need a mechanism for getting information from a text field and other form elements. Use form elements for output. Creating the HTML form. The first step in​  The Input Text object represents an HTML <input> element with type="text". Access an Input Text Object. You can access an <input> element with type="text" by using getElementById (): var x = document.getElementById("myText"); Try it Yourself » Tip: You can also access <input type="text"> by searching through the elements collection of a form.

How to Manage Text Input and Output with JavaScript for HTML5 , How to add or replace text to a text field or textarea using JavaScript. This page shows you to how to add (append) or replace text in a text area or text field. value="replace"> Replace Existing Text<br> <input type="button" value="Add New  The maximum number of characters (as UTF-16 code units) the user can enter into the text input. This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the text input has no maximum length. This value must also be greater than or equal to the value of minlength.

Comments
  • Thanks but how can I address the correct input field? It doesn't have an ID because I created it with a plugin.
  • You could use the class or name attributes
  • @user363808 You can use the name of the input field. document.querySelector('input[name=ne]')
  • Note that querySelector in this case will get the first input.
  • It works now. I'm such an idiot. I had my javascript code in the header. Of course it can't work if the form isnt loaded yet I feel so embarres. Thanks for the help.
  • Thanks but I sadly can't add an ID to the textfield because it is generated by a plugin. Is here a way to use the class instead?
  • Yes, you can, but you have to be careful with that because class selector will return all class name matching DOM elements.
  • Sadly this doesn't work either. I didn't manage to use getElement so far with any of the suggested methods.