[Javascript] Dollar function for name=
Terry Riegel
riegel at clearimageonline.com
Thu Nov 4 15:34:48 CDT 2010
Hello All,
Way back in June I asked for a method for getting an element with name="blah"
I have finished my work with it and thought I would post here for any that may find the need for similar functionality in the future.
I created 2 functions for setting and getting the value of a form element. I did this to normalize across all of the different types of form elements like radio, checkbox, input, textarea, etc...
Here are the functions...
function setvalue(nm,v) {
var i,n=document.getElementsByName(nm);
for (i=0; i < n.length; i++) {
if (n[i].type == 'hidden' || n[i].type == 'text' || n[i].type == 'password' || n[i].type == 'textarea'){ n[i].value=v[0]; }
if (n[i].type == 'checkbox' && n[i].nodeName == 'INPUT' ){
if ( v.has(n[i].value) ) {
n[i].checked=true;
} else {
n[i].checked=false;
}
}
if (n[i].nodeName == 'SELECT'){
var z, inputArray=document.getElementsByTagName('option');
for (z=0; z < inputArray.length; z++) {
if (v.has(inputArray[z].value) ){
inputArray[z].selected=true;
} else {
inputArray[z].selected=false;
}
}
}
if (n[i].type == 'radio' && n[i].nodeName == 'INPUT'){
if ( v.has(n[i].value) ) {
n[i].checked=true;
} else {
n[i].checked=false;
}
}
}
}
function getvalue(nm) {
var i,n=document.getElementsByName(nm);
var v=[];
for (i=0; i < n.length; i++) {
if (n[i].type == 'hidden' || n[i].type == 'text' || n[i].type == 'password' || n[i].type == 'textarea'){ return n[i].value; }
if (n[i].type == 'radio' && n[i].nodeName == 'INPUT' && n[i].checked){ return n[i].value; }
if (n[i].type == 'checkbox' && n[i].nodeName == 'INPUT' && n[i].checked){ v.push(n[i].value); }
if (n[i].nodeName == 'SELECT'){
var z, inputArray=n[i].getElementsByTagName('option');
for (z=0; z < inputArray.length; z++) {
if (inputArray[z].selected){
v.push(inputArray[z].value);
}
}
}
}
return v;
}
On Jun 7, 2010, at 9:32 PM, Philip Thompson wrote:
> On Jun 7, 2010, at 7:38 PM, Terry Riegel wrote:
>
>> On Jun 7, 2010, at 8:23 PM, Philip Thompson <philthathril at gmail.com> wrote:
>>
>>> On Jun 7, 2010, at 12:32 AM, Paul Novitski wrote:
>>>
>>>> At 6/6/2010 09:46 PM, Philip Thompson wrote:
>>>>> $$('input[name=pizza]').some(function (el) {
>>>>> if (el.get('value') == 'sausage') {
>>>>> el.set('checked', true);
>>>>> return true;
>>>>> }
>>>>> return false;
>>>>> });
>>>>
>>>>
>>>> Since a radio button group can have only one option checked, we'd want to clear the 'checked' attribute for every non-sausage field:
>>>>
>>>> if (el.get('value') == 'sausage')
>>>> {
>>>> el.set('checked', true);
>>>> return true;
>>>> }
>>>> else
>>>> {
>>>> el.set('checked', false);
>>>> return false;
>>>> }
>>>>
>>>> or, more concisely although perhaps not as readably:
>>>>
>>>> var bState = (el.get('value') == 'sausage');
>>>> el.set('checked', bState);
>>>> return bState;
>>>>
>>>> Regards,
>>>>
>>>> Paul
>>>
>>> There shouldn't be a need to uncheck the other (potentially-) selected radio buttons. Selecting one will, by default, unselect the others with the same name just as if you had clicked on one.
>>>
>>> ~Philip
>>
>> Unless your a belt and suspenders type of person.
>
> Touché
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript
More information about the Javascript
mailing list