option.selected can't be changed

M

M.C

I encoutered a problem which almost drove me crazy, but let me skip my
personal problem, and show you this really strange problem.
msdn.microsoft.com told me that selected is a boolean attribute
with "false" as its default value.However, unfortunately, I found that
not only "selected" was "true" when I assigned "false" to it, but also
remained "true" all the time even when I tried to change it to "false".
here go the codes:
<body>
<form >
<select >
<option id="op" value="initialValue" selected="false">op</option>
<select>
</form>

<script>
function fun(){
op.value="modifiedValue";
var bool=(1==2);
op.selected=bool;
alert("op.selected is "+op.selected + " op.value is "+op.value);
}
fun();
</script>
</body>

I have tried to find some explanation by google but without any
result, I appriciate it if you tell me why or where i am wrong.
thank you!
 
A

Adrienne

I encoutered a problem which almost drove me crazy, but let me skip my
personal problem, and show you this really strange problem.
msdn.microsoft.com told me that selected is a boolean attribute
with "false" as its default value.However, unfortunately, I found that
not only "selected" was "true" when I assigned "false" to it, but also
remained "true" all the time even when I tried to change it to "false".
here go the codes:
<body>
<form >
<select >
<option id="op" value="initialValue" selected="false">op</option>
<select> </form>

Here's your problem
<option id="op" value="initialValue" selected>op</option>

If you are using XHTML where all attributes must have values, then you
would use selected="selected".

For checkboxes, it's checked, or checked="checked".
<script>
function fun(){
op.value="modifiedValue";
var bool=(1==2);
op.selected=bool;
alert("op.selected is "+op.selected + " op.value is "+op.value);
}
fun();
</script>
</body>

Now try running your script.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top