1

Im kinda new to js, trying to change value of "email" input field

<div class="input-group type-email">
    <div class="input-label  has-placeholder-label">
        <label for="">Your Email Address</label>
    </div>
    <div class="inputs">
        <input name="email" value="" placeholder="" type="email">
    </div>
</div>

I've found a bunch of articles saying how to find an element by id. How can I do it without id, by type maybe? Thank you.

2
  • 1
    That label's for attr is bad. You can fix it by giving the input an ID (id="email", say) and updating the for attr to refer to the ID (for="email"). w3.org/TR/WCAG20-TECHS/H44 Commented Jan 19, 2015 at 0:53
  • document.getElementsByName('email')[0], or if the control is in a form there are more convenient methods, both will work in every browser everywhere. Commented Jan 19, 2015 at 1:00

1 Answer 1

2

You can use query selectors, for example:

var input = document.querySelector("input[name=email]");
input.value = "Your new value";

More on query selectors here and also here.

Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Also consider getElementsByName.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.