[Javascript] Dollar function for name=

Philip Thompson philthathril at gmail.com
Sun Jun 6 23:46:45 CDT 2010


On Jun 2, 2010, at 9:44 AM, Rees, Mark wrote:

>> 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
> 
> Ok I'll give it a whirl, but as I read it it doesn't appear to handle the logic with radio and checkboxes.
> 
> Given...
> 
> <input type="radio" name="pizze" value="Sausage">
> <input type="radio" name="pizze" value="Pepperoni">
> <input type="radio" name="pizze" value="Ham">
> 
> 
> 
> Wouldn't your code leave it looking like...
> 
> <input type="radio" name="pizze" value="Sausage">
> <input type="radio" name="pizze" value="Sausage">
> <input type="radio" name="pizze" value="Sausage">
> 
> 
> 
> What I would want it to do is leave it looking like...
> 
> <input type="radio" name="pizze" value="Sausage" checked="true">
> <input type="radio" name="pizze" value="Pepperoni">
> <input type="radio" name="pizze" value="Ham">
> 
> 
> 
> Thanks,
> 
> Terry
> 
> -------------------------------
> I see, I think. Again the code is untested. 
> 
> //find all inputs which have the name of pizze and the value of Sausage
> $$('input[name="pizze"][value="Sausage"]').each(function(el){
> 	//and write the "checked" attribute on each one
> 	el.writeAttribute('checked','checked');	
> });
> 
> Read the docs and try some of this out for yourself. There are loads of examples out there. Here's one
> http://alternateidea.com/blog/articles/2006/3/27/prototype-gets-attribute-selectors

Along the same lines using selectors (in Mootools):

$$('input[name=pizza]').some(function (el) {
    if (el.get('value') == 'sausage') {
        el.set('checked', true);
        return true;
    }
    return false;
});

Hope this helps.

~Philip


More information about the Javascript mailing list