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