473,703 Members | 2,660 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 1939
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
4099
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
10804
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 method instead of QueryString as I don't want the carried variable to be visible for the user.
4
2264
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
4312
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, System.EventArgs e)
1
3413
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 postback, I can't see any of my dynamically created HTML controls in VB .NET. How do I make them...
5
2874
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
6526
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 page. I am able to call values from file in my jsp page. But as dynamic values can be any in no...
4
2713
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 that apply.
2
3384
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 example) except for something related to the function behind my link. The link simply calls a function...
0
8761
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 usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8672
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9017
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8969
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5923
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2462
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.