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 (email@example.com) like this:
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','');
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.value = "Your value";
- 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
- @user363808 You can use the name of the input field.
- Note that
querySelectorin this case will get the first
- 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.