[Javascript] Dollar function for name=

Philip Thompson philthathril at gmail.com
Tue Jun 1 14:42:56 CDT 2010


On Jun 1, 2010, at 10:15 AM, Terry Riegel wrote:

> I think my post may have been a bit premature, as a bit of googling found document.getElementsByName() which returns  all elements with that name. In most of my work name="" are unique in the same way id="" is unique, so for me this function may be all I need...
> 
> function $name(n){
> if (typeof n == 'object') {
>  return n;
> } else {
>  return document.getElementsByName(n)[0];
> }
> }
> 
> Terry

Here is an instance that's common and not unique for "name":

<input type="radio" name="pizza" value="sausage" />
<input type="radio" name="pizza" value="pepperoni" />
<input type="radio" name="pizza" value="cheese" />

If you have the option (and if it's needed), assign an id to each input - this will ensure that you're getting the correct element.

Hope that helps.
~Philip


> On Jun 1, 2010, at 11:06 AM, Terry Riegel wrote:
> 
>> Hello All,
>> 
>> Is there a method for finding a name="whatever" on a page similar to document.getElementById('whatever')?
>> 
>> For example suppose I have this form...
>> 
>> 
>> <form action="go.html" method="post">
>> <input type="text" name="myname">
>> <input type="text" name="mypass">
>> <input type="submit" name="mysubmit" value="Login">
>> </form>
>> 
>> 
>> I would like to target name="myname" in a similar way that I can with getElementById()
>> 
>> Even if it takes a fair bit of code to do it it would be nice to have a 'dollar' type function for this.
>> 
>> When I use the term dollar function I am referring to the fairly ubiquitous convenience function for getElementById() found in the popular javascript library prototype.
>> 
>> Thanks,
>> 
>> Terry Riegel

"innerHTML is a string. The DOM is not a string, it's a hierarchal object structure. Shoving a string into an object is impure and similar to wrapping a spaghetti noodle around an orange and calling it lunch."



More information about the Javascript mailing list