0

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

4
  • Actually, IDs are not allowed to start with a digit (just FYI): w3.org/TR/html401/types.html#type-name Commented Sep 3, 2010 at 21:58
  • 2
    @Felix - It's worth noting at this point that the rules change with HTML5: dev.w3.org/html5/spec/elements.html#the-id-attribute As long as there are no spaces and it's unique, it works. Commented Sep 3, 2010 at 22:02
  • hmmm... That is news to me. I will certainly change that up to start with a letter. Thanks! Commented Sep 3, 2010 at 22:03
  • @Nick Craver: Thanks, I didn't know :) Commented Sep 3, 2010 at 22:08

2 Answers 2

3

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.

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

8 Comments

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 - 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.
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 - IDs have to be unique, so remove those and use the names instead, like this: $('input[name="' + myArray[i] + '"]:checked').val()
@Jeff - Yup, radio buttons are meant to be grouped by name, just removing IDs is probably what you're after in this case
|
2

You forgot a closing parenthesis.

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

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

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.