[Javascript] Dollar function for name=

Terry Riegel riegel at clearimageonline.com
Tue Jun 1 16:07:24 CDT 2010


Phillip,

Thanks for the info. I may be able to still do what I am needing even in that situation. I will have to think it through.

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.

Any input regarding this will be appreciated.


function $name(n){
 if (typeof n == 'object') {
  return n;
 } else {
  return document.getElementsByName(n)[0];
 }
}

What if my $name() function creates (if it doesn't exist) an object that acts as if $name('pizza') is unique in the same way an ID is unique? And then I could get $name('pizza').value




Terry




On Jun 1, 2010, at 3:42 PM, Philip Thompson wrote:

> 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."
> 
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript



More information about the Javascript mailing list