PDA

View Full Version : Form object question about SELECT



DigitalBard
08-21-2008, 06:00 PM
Can you enable a value in a form SELECT object by name?

I tried:

document.MyForm.MySelect['yes'].selected = true

but I got an error.

Twey
08-21-2008, 06:12 PM
<option> elements can't have names.

Use document.forms[yourFormId].elements[yourElementId] to access an element, to minimise accidental collisions.

DigitalBard
08-21-2008, 07:14 PM
Thanks, I ended up just looping through the whole thing to select the items I needed:



// sSel - name of the SELECT element in your form
// sStr - elements of the SELECT you want selected, separated by commas
function selectFromStr(sSel, sStr)
{
var o = document.getElementById(sSel);
var sel = sStr.split(', ');
for(i=0;i<o.length;i++)
{
o.options[i].selected = false
for(s=0;s<sel.length;s++)
{
if (o.options[i].text==sel[s]) o.options[i].selected = true
}
}
}

Twey
08-21-2008, 11:26 PM
Hmm... note that that's a name you should be passing there, not an ID.