473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Dynamically set value in htmlselectelement via DOM?

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

Feb 9 '06 #1
1 1923
ke******@gmail.com wrote:
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[i].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[i].value == the_model){
model.selectedIndex = i
}
}
}

--
Rob
Feb 9 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bill M. | last post by:
Hello, I would like to extend or sub-class the base HTMLSelectElement and add some custom properties and methods. So far this works to create a new select element. var new_select= new...
3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
4
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: Marcus | last post by:
I have a problem maybe one of you could help me with. I've created a data entry screen with lots of dynamically-created client-side controls. I create HTML texboxes client-side by assigning a...
5
by: stellstarin | last post by:
I have a html where fields are created and added dynamically on the client side. I use the AppendChild() call to create fields dynamically. On submit i try to get the value for all the...
1
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
4
by: assgar | last post by:
Hi I am stuck on a problem. I use 3 scripts(form, function and process). Development on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. The form displays...
2
by: jmarendo | last post by:
Hello, After reading through the "Table Basics - DOM - Refer to table cells" example at mredkj.com , I modified the code for my own purposes. In the modified version, I create a hyperlink and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.