0

I was trying to add the password into an input without class or id.

This is my code:

<form>
  <div class="something"> </div>
  <input type="password" name="password" placeholder="password">
  <input type="submit" value="Watch Video">
</form>

And i was trying something like this:

jQuery(document.ready(function($){
   $('.something').next().write('password'); 
});

How can i make it? And after put the password, can i submit the next input?

1
  • Tiny point but in this case I'd suggest div#something would be more appropriate than div.something, as I'd infer you want to refer to this single, unique element. Commented Jun 25, 2015 at 11:52

3 Answers 3

2

In order to change the value of an input field you need to pass your string to the value property.

$('.something').next().prop('value', 'password'); 

Or you can use the shorthand val() method that populates the value property:

$('.something').next().val('password'); 

I would also recommend not using the next function because this means you are 100% dependent on the ordering of the HTML elements. I would suggest using a selector like this:

$('.something').siblings('input:password').val('password'); 

This selector will always work as long as the password field is in the same hierarchy as the .something element.

Reference - https://api.jquery.com/siblings/

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

1 Comment

Well explained , thanks for the tip about depend on the order of HTML, but in this case, the order will be the same 100%, but thanks a lot!
2

You set the value of input elements with val, not write (there is no jQuery write function). Other than that, your code is correct:

jQuery(document.ready(function($){
   $('.something').next().val('password'); 
   // --------------------^
});

Comments

2

Use val() instead of write.

 $('.something').next().val('password');

The val() method returns or sets the value attribute of the selected elements.

When used to return value: This method returns the value of the value attribute of the FIRST matched element.

When used to set value: This method sets the value of the value attribute for ALL matched elements.

Note: The val() method is mostly used with HTML form elements.

Comments

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.