getElementsByID Question

Hey,

I have dynamically named elements and I was wondering if jquery has a way of using a wildcard so I can create a var dynamically for an form id element that is chosen by a visitor. For instance, if I have 10 submit buttons with different names, I can take action by using this syntax:

$("#[id^=button_name_]").click(function() {

the _ prefix allows me to assign a dynamic number to the name so as long as one of those forms is submitted, I am guaranteed the ability to perform an action. Well, it got me to thinking "Can I do this with capturing values of dynamic elements?

Imagine I want to get the value of an ID but I won’t know the name ahead of time. All I will know is that out of 10 records, the ID’s will be named:

element_id_1
element_id_2

element_id_10

Can I do something (forgive the pseudo code) like this to capture it?

getElementbyID(“^=element_id_”)?

The goal is to create a var that represents a dynamically selected ID with a dynamic name generated from looping through a db. Is this possible in jquery?

Thought:

What if I did something like this:

var arr = new Array();
arr = document.getElementsByName( “element_id” );

Would I then be able to set a var for the array that does have a value? This way (I am using form elements) I can set a value for the one selected element and not carry the value of 9 others who have 0 value/empty value.

How do you do that in javascript?

Can’t you like this?


var eles = $("input[name^=my_element_]");
alert(eles.length);

And/or loop with jquery.each.

That doesn’t quiet work (it’s because I didn’t properly explain my situation). I should be more clear. Here is what I have:

I have radio buttons but they work differently (I know it’s not conventional but I will skip past that as it is discussed in another thread).

They appear as such:


<li id="[B]checkbox5_3[/B]"><a href="javascript:upny_captVote('choice_3', 'checkbox5_3', '109');"><span>Hey, Why Not?!</span></a></li>

For the sake of making sense of it all, let’s just say it does work as our site wants it now but the goal is to know when that element checkbox is selected

I have an included .js file with functions that can read it but I want to be
able to do it from another file that handles the jquery (long story)

I want to simulate a function like this which exists in a another .js file:

function upny_EleObjs(e) { if(typeof(e)=='string') { if(document.getElementById) { e=document.getElementById(e); } else if(document.all) { e=document.all[e]; } else { e=null; } } return e; }

Then call it such as:

var v=upny_EleObjs(hiddeninput_id).value;
alert (v);

The trick is getting the 'hiddeninput_id) to be the <li> that is selected.

Everything else I have works. It’s just that this value, having it as a var that I can work with, would make it even more powerful. Does this help explain my situation better? I apologize for not being clearer earlier and I do thank you, again, for your time.

PS. In case you are curious, the reason I want the var of that selected <li> checkbox is because I can pass it to my other functions easily (once I have the var) to do background work which would allow a more efficient operation. While the site can work without it, it would save a ton of time. I just can’t quite figure out how to get it to work without hiddeninput_id being ‘undefined’ or getting ‘undefined’ popups when I try from scratch to do it.

Here is what I have. It alerts the first instance, not the one
that is attached to the one that is selected.

window.alert($(‘input[name=li_choice]’).val());

For each <li> element I have attached a hidden field (trying something new)
to see if I could get that to work. The hidden field attached to each <li> checkbox is:

<input type=“hidden” name=“li_choice” value=“<?=$cid;?>”>

Possible? Or is there a better way? That is all I am looking to do. Ty, again.

PS. To grab the li itself I have tried:

window.alert($(‘input[name^=checkbox_]:checked’).val());

But since it is itself not ‘input’ it alerts (undefined)

Ignore the 2 posts above, please:

I apologize for another post, I am not able to edit the above 2 (I wanted to delete them)

I thought about this some more and realize all I want, if possible, is this:

Each survey I have has a hidden field (question_id)
I just want to identify which one was chosen at that given time.

<input type=“hidden” name=“question_id” value=“4”>

For instance, that is what would appear for one of the surveys, where Question ID = 4

If that one is the hidden field in the ‘survey’ selected, how does jquery recognize that and ignore the other hidden fields that are blank?

Thanks