473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically set value in htmlselecteleme nt 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_mo del) {
alert("in set model");
var model = $('model'); //shortcut provided by prototype.js
alert(model);
var modelre = new RegExp(the_mode l);
// 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.selectedI ndex = i;
}
}
}

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

Thanks,
Kevin

Feb 9 '06 #1
1 1935
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_mo del) {
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('thund erbird');
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_mode l);
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.optio ns.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.selectedI ndex = i;
}
if (model.options[i].value == the_model){
model.selectedI ndex = i
}
}
}

--
Rob
Feb 9 '06 #2

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

Similar topics

5
4083
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 Selector('new_select'); Selector = function(id) { var container = document.getElementById(id);
3
10785
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 then like that, when clicking the LinkButton, the user can be navigated to another page, carrying a variable. I would like to use server.transfer...
4
2248
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 source:
8
4302
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 button at runtime: //----- Code snippet protected System.Web.UI.WebControls.PlaceHolder ImageHolder; private void Page_Load(object sender,...
1
3401
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 value to the td.innerHTML property. The UI is done, and I now want to post back the user's changes and update my business object in .NET. But when I...
5
2858
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 elements in the form, including those that are added dynamically. I use document.getElementsByName('Field Name')to achieve the same.
1
6491
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 java class which creates a xml file based on values entered in dynamic jsp page. Now i want to read all those values entered to xml in my other jsp...
4
2701
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 multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees...
2
3373
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 place it in the last cell of each row that I create dynamically using DOM methods. Everything is working well (that is, just like the original...
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.