Dynamically set value in htmlselectelement via DOM?

K

kevinold

Hello everyone,

I'm using the prototype.js libaries and am trying to parse a select
element that has been filled in via an ajax call (returning html of
<options>). I'm trying to loop through all the elements and set the
selectedIndex of the element, but it doesn't seem to be working.
Here's the code snippet. Let me know if I need to post more code:

var the_model = 'thunderbird';

function setModel(the_model) {
alert("in set model");
var model = $('model'); //shortcut provided by prototype.js
alert(model);
var modelre = new RegExp(the_model);
// Look through the models and match 'thunderbird'
for (var i=model.options.length-1; i>=0; i--) {
var m = model.options.value;
alert(m);
if ( modelre.test(m) ) {
model.selectedIndex = i;
}
}
}

I'd like to do this the true DOM way. Any help is appreciated!

Thanks,
Kevin
 
R

RobG

Hello everyone,

I'm using the prototype.js libaries and am trying to parse a select
element that has been filled in via an ajax call (returning html of
<options>). I'm trying to loop through all the elements and set the
selectedIndex of the element, but it doesn't seem to be working.
Here's the code snippet. Let me know if I need to post more code:

var the_model = 'thunderbird';

Here you set a global variable with a value of 'thunderbird'...

function setModel(the_model) {

Here you create a local variable with the same name as a global variable
- it will mask the global when you call the function. Presumably you
are calling this with:

setModel('thunderbird');


in which case the global should be removed. If you expect the function
to use the global variable, remove the parameter from the function
declaration:

function setModel() {

and call it with:

setModel();

alert("in set model");
var model = $('model'); //shortcut provided by prototype.js
alert(model);
var modelre = new RegExp(the_model);

There is no need for a regular expression if you are doing a simple
string equivalence test. If you are doing something else (e.g.
'thunderbird' is only part of the string you want to match) then maybe a
RegExp is required.

// Look through the models and match 'thunderbird'
for (var i=model.options.length-1; i>=0; i--) {

That's a bit convoluted, try:

for (var i=0, len=model.options.length; i<len; ++i) {

var m = model.options.value;
alert(m);


The 'var m...' line is not required.
if ( modelre.test(m) ) {

Using the RegExp and test will match the string 'thunderbird' anywhere
in the value of the option (say in the string 'I like thunderbird ...').
model.selectedIndex = i;
}

if (model.options.value == the_model){
model.selectedIndex = i
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top