Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamically set value in htmlselectelement via DOM?

kevinold@gmail.com
Guest
 
Posts: n/a
#1: Feb 9 '06
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[i].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


RobG
Guest
 
Posts: n/a
#2: Feb 9 '06

re: Dynamically set value in htmlselectelement via DOM?


kevinold@gmail.com wrote:[color=blue]
> 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';[/color]

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

[color=blue]
> function setModel(the_model) {[/color]

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();

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

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.

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

That's a bit convoluted, try:

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

[color=blue]
> var m = model.options[i].value;
> alert(m);[/color]

The 'var m...' line is not required.
[color=blue]
> if ( modelre.test(m) ) {[/color]

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

if (model.options[i].value == the_model){
model.selectedIndex = i
}
[color=blue]
> }
> }[/color]


--
Rob
Closed Thread