/    Sign up×
Community /Pin to ProfileBookmark

Assign value attributes to an option

I’m experiencing the following problem and would be pleased if someone helps me solve it. See this code:

[CODE]
opt = document.createElement(“option”);
opt.setAttribute(“value”, varValue);
opt.appendChild(document.createTextNode(“option text”));
[/CODE]

Meanwhile varValue has always a numeric value. Here’s the point where Internet Explorer 6.0 doesn’t go further ? A get the following error message: “Invalid property value”. Please help me! ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JUDJul 29.2006 — Can't really help much with that little bit of code because it looks OK. There must be something else wrong somewhere else in your code.

Here's a little example I've made that may help you. It uses your code to add options 1 - 10 to a select list and uses the onchange event handler to change the textbox value to the value of your selection.

[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
// <![CDATA[
onload = function(){
for(var i = 1; i <= 10; i++){
var varValue = i;
var opt = document.createElement("OPTION");
opt.setAttribute("value", varValue);
opt.appendChild(document.createTextNode(i));

document.forms[0].selection.appendChild(opt);
}
}
// ]]>
</script>
</head>

<body>
<form>
<select name="selection" onchange="this.form.output.value = this.options[this.selectedIndex].value">
<option>Plesae select an option</option>
</select><br />
<input type="text" name="output" />
</form>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@xploadauthorJul 29.2006 — Thank you very much for the example code, JUD, although you're right - the problem is elsewhere. Infortunately it is a complicated JS/AJAX/DOM system and I don't wont to bore anyone with my code(it includes also PHP extension Smarty so the code is server-side generated).

Now I see another failure(probably the reason why my site crashes). Take a look at this:
[CODE]
td = document.createElement("td");
td.style.textAlign = "right";
td.style.backgroundColor = "#F6F6F6;";
[/CODE]

This time I get following error message: "Member not found". Should I access the style attribute due document.all or what? Under Firefox everything's fine.
Copy linkTweet thisAlerts:
@JUDJul 29.2006 — Try posting your complete code. Can't really help with just three lines of your code because, again, the code looks fine. I'm sure you won't bore anyone. People are here to help eachother after all.
Copy linkTweet thisAlerts:
@xploadauthorJul 30.2006 — I think I found the bug. It's the semicolon at the end of the color value here:

td.style.backgroundColor = "#F6F6F6;";

Thanks once again for your help ?
×

Success!

Help @xpload spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...