views:

37

answers:

2

I have loaded an array in javascript with a select amount of input element ids on the form. Some of the input elements are radio buttons & checkboxes,.

What I would like to do is something jQuery where I'm looping through my array and passing in the id like this:

for(i = 0; i <= myArray.length - 1;i++){
alert($(myArray[i].val());
}

I'm getting an error when I do this. Is there a way to do what I want?

UPDATE:

My array contains the IDs of the elements. My elements have numeric values as their IDs. For example in my array #50:0 and #51:0 are elements of the array.

My HTML looks like this:

<input id="50:0" type="radio" value="1" name="50:0">Yes
<input id="50:0" type="radio" value="2" name="50:0">No

<input id="51:0" type="checkbox" value="1" name="51:0">Option 1
<input id="51:0" type="checkbox" value="2" name="51:0" >Option 2
<input id="51:0" type="checkbox" value="3" name="51:0" >Option 3

Assume that you clicked Yes and Options 1 & 3 I need those values...

+3  A: 

Your syntax is a little off, it should be this:

for(i = 0; i < myArray.length; i++){
  alert($(myArray[i]).val());
}

The $(selector) was missing it's closing ). Also make sure your elements in the array are prefixed with a #, otherwise you'll need $('#' + myArray[i]) instead, to make it an #ID selector.

And a readability tip, you can replace i <= myArray.length - 1 with just i < myArray.length.

Nick Craver
Thanks for the heads up... When I try to alert out the val() I get an undefined... Specifically the element is a type of checkbox. I've also tried to do a radio button and got the same results.
Jeff V
@Jeff - Are you using ASP.Net? Prior to 4.0, they don't have a value client-side, look at your markup and check for the `value=""` attribute.
Nick Craver
I'm able to hack with regular javascript and get the values for each element. I'm really hoping there is something cleaner by using jQuery... I am using VS2010 but I'm really displaying everything via jQuery and JSON. All the elements are dynamic (I'm writing the HTML on the fly).
Jeff V
@Jeff - Are you running this in a `document.ready` handler, so the elements are there before it loops? If not the selectors probably aren't finding any elements :)
Nick Craver
@Nick yeah I am doing the document.ready and the same issue is occurring... What is need to get is the selected (or checked) value(s)... I updated the original question too.
Jeff V
@Jeff - IDs have to be unique, so remove those and use the names instead, like this: `$('input[name="' + myArray[i] + '"]:checked').val()`
Nick Craver
@Nick - I will try that. So it sounds like I should re-write the way I am displaying my elements. Should I just use the name attribute?
Jeff V
@Jeff - Yup, radio buttons are meant to be grouped by name, just removing IDs is *probably* what you're after in this case
Nick Craver
+2  A: 

You forgot a closing parenthesis.

alert($(myArray[i]).val());

And make sure that you strings are #foo and not just foo.

Matti Virkkunen