[Javascript] Dollar function for name=

Rees, Mark Mark.Rees at astrazeneca.com
Wed Jun 2 09:23:27 CDT 2010




--------------------------------------------------------------------------
AstraZeneca UK Limited is a company incorporated in England and Wales with registered number: 03674842 and a registered office at 15 Stanhope Gate, London W1K 1LN.
Confidentiality Notice: This message is private and may contain confidential, proprietary and legally privileged information. If you have received this message in error, please notify us and remove it from your system and note that you must not copy, distribute or take any action in reliance on it. Any unauthorised use or disclosure of the contents of this message is not permitted and may be unlawful.
Disclaimer: Email messages may be subject to delays, interception, non-delivery and unauthorised alterations. Therefore, information expressed in this message is not given or endorsed by AstraZeneca UK Limited unless otherwise notified by an authorised representative independent of this message. No contractual relationship is created by this message by any person unless specifically indicated by agreement in writing other than email.
Monitoring: AstraZeneca UK Limited may monitor email traffic data and content for the purposes of the prevention and detection of crime, ensuring the security of our computer systems and checking Compliance with our Code of Conduct and Policies.
-----Original Message-----
From: javascript-bounces at lists.evolt.org
[mailto:javascript-bounces at lists.evolt.org]On Behalf Of Terry Riegel
Sent: 02 June 2010 15:19
To: JavaScript List
Subject: Re: [Javascript] Dollar function for name=



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

Try this then, again from Prototype since that's what you're using

http://api.prototypejs.org/dom/element/writeattribute/

so once more:

 $$('input[name="pizze"]').each(function(el){
  	el.writeAttribute('value','sausage');
 });

Not quite as neat as jquery though


More information about the Javascript mailing list