473,504 Members | 13,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select option change using DOM

Hi All,

As far as my understanding of HTML DOM aka DHTML goes, is that if the
DOM structure of HTML document is changed programmatically using
JavaScript in the browser, it immediately gets reflected in the page's
view. For example, if following code is executed on say a click of a
button

var f = document.getElementById("f1"); // f1 is id of a form
var i = document.createElement("input");
i.setAttribute("type", "text");
f.appendChild(i);

the page immediately shows the new textbox under that form. But in the
following code, this doesnt happen.

<form id="f1">
<select id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</form>
<script>
// Gets executed say on click of some button
var c = document.getElementById("cars");
c.options[1].setAttribute("selected", "selected"); // doesnt work
c.options[1].selected = true; // works
</script>

the option in combo box remains same. I checked in Firebug that the
attribute is added properly. Using the selected attribute works. Why
so?

Thanks in advance,
Manish

Apr 16 '07 #1
2 11239
On Apr 17, 12:09 am, "Manish Tomar" <manish.to...@gmail.comwrote:
<form id="f1">
<select id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</form>
<script>
// Gets executed say on click of some button
var c = document.getElementById("cars");
c.options[1].setAttribute("selected", "selected"); // doesnt work
c.selectedIndex = 1;
Pasted from W3C's DOM2-HTML Specification:
<url http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-70874476>

Interface HTMLOptionElement

selected of type boolean
Represents the current state of the corresponding form control, in an
interactive user agent. Changing this attribute changes the state of
the form control, but does not change the value of the HTML selected
attribute of the element.

HTH

Apr 17 '07 #2
Thanks :) I checked it out at given link. defaultSelected property
represents the html "selected" attribute and doesn't help in changing
the control.

On Apr 17, 6:02 am, Cah Sableng <cahsabl...@gmail.comwrote:
On Apr 17, 12:09 am, "Manish Tomar" <manish.to...@gmail.comwrote:
<form id="f1">
<select id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</form>
<script>
// Gets executed say on click of some button
var c = document.getElementById("cars");
c.options[1].setAttribute("selected", "selected"); // doesnt work

c.selectedIndex = 1;

Pasted from W3C's DOM2-HTML Specification:
<urlhttp://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-70874476>

Interface HTMLOptionElement

selected of type boolean
Represents the current state of the corresponding form control, in an
interactive user agent. Changing this attribute changes the state of
the form control, but does not change the value of the HTML selected
attribute of the element.

HTH

Apr 17 '07 #3

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

Similar topics

12
6508
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
4
102922
by: Jeff Anderson | last post by:
Is there a style for a select option's "selected" color? For example: <HTML> <BODY> <FORM NAME="form1"> <SELECT NAME="mySelect" SIZE="7" style="background-color:red;"> <OPTION>Test 1...
6
2379
by: Bonge Boo! | last post by:
This has got to be obvious, but I can't make it work. I have a form called with 3 pull down menus. They are linked to a database which generates the values for the <SELECT? Pull-downs. Lets...
7
1894
by: sean | last post by:
SELECT drop-list width incorrect for long text when SELECT is fixed width Setting the width style of a SELECT statement causes the contents of the drop-list to be cropped. This is problematic...
1
2949
by: Joe Attardi | last post by:
Hi all, On a form on one of my pages I have two <select> elements, and each one is paired up with a radio button. The idea is to choose an item from one list or the other and select the radio...
5
8720
by: Isha | last post by:
Hi all, I wanted to change the background color for only the first option in a select box, but following changed the background color for the whole dropdown box. <select name="alltags"...
2
2813
by: entfred | last post by:
I was experimenting with trying to select the same item in a select box twice in a row and found out that you need to do a refresh (view - refresh) in Internet Explorer. This is so you can click...
21
29739
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays...
4
6075
by: html | last post by:
Hello all, I need to change the colour of my text. For the paragraph I do document.fgColor ="blue" ; but how do I do this for the select tags? ....and I prefer it to apply to all select tags...
0
7213
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,...
1
7017
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...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
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...
0
4698
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...
0
3187
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...
0
1526
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 ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
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...

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.