• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 99
  • Last Modified:

Set / retrieve value of array of inputs

I have a webpage with an array of inputs named 'myinputs'

Is there anyway to set the 4th one to the value of 15?
0
Sheldon Livingston
Asked:
Sheldon Livingston
  • 5
  • 4
1 Solution
 
chaauCommented:
First of all you need to access the inputs by name. In jQuery you do it like this:
$("name='myinputs'")

Open in new window

This will return you the array of jQuery objects.
You can then access the 4th element and set the value to 15, like this:
$("name='myinputs'")[3].val('15');

Open in new window

However, I suggest you test that the array size is at least 4:
if($("name='myinputs'").length>3)
  $("name='myinputs'")[3].val('15');

Open in new window

0
 
Sheldon LivingstonConsultantAuthor Commented:
No joy... syntax error.

$("name='theVendorID'");
$("name='theVendorID'")[3].val('15');
alert($("name='theVendorID'")[3].val());

Open in new window

0
 
chaauCommented:
Sorry, you need to use eq() function:
if($("[name='myinputs']").length>3)
  $("[name='myinputs']").eq(3).val('15');

Open in new window

Or, like this:
$("[name='myinputs'].eq(3)").val('15');

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Sheldon LivingstonConsultantAuthor Commented:
I've been guessing at this all night... googled 50 or so pages.

Maybe this is just too complex for jquery.

Anyway... eq failed too.

$("name='theVendorID'");
$("name='theVendorID'").eq(3).val('15');
alert($("name='theVendorID'").eq(3).val());

Open in new window


Syntax error.  I'd thing this would be a good thing to update jquery to be able to do...
0
 
chaauCommented:
Please check my answer again. I have added the square brackets around [name='myinputs']. This is the correct syntax. Yours should be:
alert($("[name='theVendorID']").eq(3).val());

Open in new window

or
alert($("[name='theVendorID']:eq(3)").val());

Open in new window

0
 
Sheldon LivingstonConsultantAuthor Commented:
Here's my code... still doesn't work.

$("[name='theVendorID']");
$("[name='theVendorID']").eq(3).val('15');
alert($("[name='theVendorID']").eq(3).val());

Open in new window

0
 
chaauCommented:
show how the input elements are created. Even better, create a JSFiddle, like this
0
 
chaauCommented:
Have a look at this
0
 
Sheldon LivingstonConsultantAuthor Commented:
jquery ios lame
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now