[Javascript] Dollar function for name=
Terry Riegel
riegel at clearimageonline.com
Wed Jun 2 09:18:31 CDT 2010
On Jun 2, 2010, at 9:58 AM, Rees, Mark wrote:
>
>> On Jun 1, 2010, at 3:07 PM, Terry Riegel wrote:
>>
>>> I want to be able to do something like...
>>>
>>> $name(n).value="sausage";
>>>
>>> In the case of a radio button it would be to set the radio whose name is pizza and whose value is sausage to selected
>>>
>>> I will have to think through how this may/may not work. It is really something specific to my thinking and for me to be able to create a convenience function to let me address elements by their name instead of having to create an ID for all of them.
>>>
>>> So in the case of an input I am thinking in terms of an "pseudo" object/elment that will let me deal with all the radio buttons as a single object/element.
>>
>>
>> I'm not generally one to point to frameworks when the question is about JavaScript, but your phrasing just screams framework. Pseudo-elements to refer to multiple elements is exactly what JavaScript frameworks provide. Here's how you'd do this in jQuery, for example:
>>
>> $('[name=n]').val('sausage');
>>
>> Peace,
>> Scott
>
>> Exactly, the frame work I use is prototype, but I have not been able to find this kind of functionality. So I thought I would try to >>>>develop it. :)
>
>> Terry
>
> -----------------------------------------------------
>
>
> What was so wrong with the double dollar selector? Did you want a code example?
>
> Untested so may have a syntax error, but you will get the idea
>
> $$('input[name="sausage"]').each(function(el){
> //do what you want with the element(s) here
> //obviously there can be more than one element with the requested name as many other posters have noted
> alert(el.id);
> });
>
> And once again, the references. They are really very useful for using double dollar
>
> http://api.prototypejs.org/language/dollardollar/
>
> http://www.css3.info/preview/attribute-selectors/
The double dollar selector would be fine, but I can also get to it with document.getElementsByName("sausage")
I am more interested in the generalized way of dealing with input elements like text, textarea, radio, checkbox, select etc. To be able to set and get their values in a consistent way.
Thanks,
Terry
More information about the Javascript
mailing list