1

$("[name=imageheight]") return following array of textboxes

enter image description here

I know i can get the value of textbox by its index like

enter image description here

How do i set the value of textbox on index 0. i have tried this but it gives me a error

enter image description here

5
  • 3
    $('[name=imageheight]').eq(0).val() Commented Sep 15, 2016 at 9:15
  • stackoverflow.com/questions/1107220/… Commented Sep 15, 2016 at 9:16
  • thanks @guradio.. $('[name=imageheight]').eq(0).val() run without any error but $("[name=imageheight]") still return " [<input id=​"2793497" type=​"number" name=​"imageheight" value=​"175">​,<input id=​"4346700" type=​"number" name=​"imageheight" value=​"175">​]" Commented Sep 15, 2016 at 9:23
  • can you update the OP so it is more clear what is still the problem Commented Sep 15, 2016 at 9:26
  • $('[name=imageheight]').eq(0).attr('value', '250') its working for me. Thanks for your help... Commented Sep 15, 2016 at 10:39

6 Answers 6

1
$('[name=imageheight]').eq(0).attr('value', '250')
Sign up to request clarification or add additional context in comments.

Comments

0

$('input').eq(0).val("new value")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />

2 Comments

You should explain your answer.
I don't understand this answer, no explaination and I don't see the problem solve and make me confused.
0

you can use

$('[name=imageheight]').eq(0).val(250)

Comments

0

The problem in your code is, $("[name=imageheight]")[0] will not return an jQuery object. So you cannot use .val() over that node. But, $("[name=imageheight]").eq(0) will return the node as a jQuery object over which you can use .val()

$('input').eq(0).val("new value")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />

Comments

0

Actually $("[name=imageheight]") returns jquery object of that element. that jquery object contains javascript DomElement object you can access DomElement object from jQuery object by $('....')[0]

$("[name=imageheight]")[0].value obviously return value because value is propert of DomElement.

You can set value by $("[name=imageheight]")[0].value= 250; in DomElement object

val() is setter and getter method of jQuery you have to fetch first element onlu by .first() method or ':first' selector.

$("[name=imageheight]:first").val(250);

or

$("[name=imageheight]").first().val(250);

Comments

0

Explanation:

jQuery returns a jQuery object which has a val function, which returns its value if no parameter is passed and sets its value if a parameter is passed. However, the [0] of a jQuery object is an element and is not a jQuery object. As a result, it does not have a val() function. You can work with that as well, if you set its value attribute, like this:

jQuery("input[name=firstname]")[0].value = 250; Also, you can bypass jQuery if you want, like this:

document.querySelectorAll("input[name=firstname]")[0].value = 250;

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.