Link to home
Start Free TrialLog in
Avatar of Sheldon Livingston
Sheldon LivingstonFlag for United States of America

asked on

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?
Avatar of chaau
chaau
Flag of Australia image

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

Avatar of Sheldon Livingston

ASKER

No joy... syntax error.

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

Open in new window

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

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...
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

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

show how the input elements are created. Even better, create a JSFiddle, like this
ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jquery ios lame