/    Sign up×
Community /Pin to ProfileBookmark

help with appending options to select element

hi, all
i have a problem with appending options from object to select element
i append options but it shows empty in the select list
here is my script

[code=php]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“http://www.w3.org/TR/html4/strict.dtd”
>
<html lang=”en”>
<head>
<title><!– Insert your title here –></title>
<script>
var mehsullar = [
{ name:’MotherBoard ASUS’, value:’MotherBoard ASUS’, option:null },
{ name:’MotherBoard Gigabyte’, value:’MotherBoard Gigabyte’, option:null },
{ name:’MotherBoard MSI’, value:’MotherBoard MSI’, option:null },
{ name:’Graphiqcard ASUS’, value:’Graphiqcard ASUS’, option:null },
{ name:’GraphigCard ATI’, value:’GraphigCard ATI’, option:null },
{ name:’GraphigCard GefORCE’, value:’GraphigCard GefORCE’, option:null }
];

mehsullar.artim=function(menu)
{

this.menu=menu;
// this.cedvel=cedvel;
for(var i=0;i<this.length;i++){

this[i].option=document.createElement(‘option’);
this[i].option.name=this[i].name;
this[i].option.value=this[i].value;
this.menu.appendChild(this[i].option);

}

alert(this.length)
}

onload = function() {
var menu=document.getElementById(‘siyahi’);

mehsullar.artim(menu);

}
</script>
</head>
<body>
<form name=”prod”>
<table><tr><td>say</td><td>
<select id=”siyahi”><option value=””>fhfdh fh fh</option></select></td></tr>

</form>
</body>
</html>
[/code]

thanks in advance for attention

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERApr 08.2011 — Try using a more recent version...
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;title&gt;1-level Drop Down Selection&lt;/title&gt;

&lt;script type="text/javascript"&gt;
// For: http://www.webdeveloper.com/forum/showthread.php?t=244762

var selOptions = [
'MotherBoard ASUS', 'MotherBoard Gigabyte',
'MotherBoard MSI', 'Graphiqcard ASUS',
'GraphigCard ATI', 'GraphigCard GefORCE'
];

function initSel() {
oSel1 = document.getElementById('sel1');
oSel1.options[oSel1.options.length] = new Option('Select an option','',false,false);
for (i=0; i &lt; selOptions.length; i++) {
oSel1.options[oSel1.options.length] = new Option(selOptions[i],selOptions[i], false,false);
}
}

window.onload=function() {
initSel();
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;select id="sel1"&gt;&lt;/select&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@azegurbauthorApr 10.2011 — Thank you very much. but it isnt possible to do that both OOP and appendChild() function. what is 4 paramaters inside new Option()? Can you explain it me. Thanks again very much for your attention.
Copy linkTweet thisAlerts:
@JMRKERApr 10.2011 — Thank you very much. but it isnt possible to do that both OOP and appendChild() function. what is 4 paramaters inside new Option()? Can you explain it me. Thanks again very much for your attention.[/QUOTE]

You're most welcome. Happy to help.

  • 1. Yes, it is possible, but I tend to bend to the KISS principle unless otherwise contra-indicated.


  • 2. The last two parameters are optional. I like to keep them as a remindeer in case I wish to used them later. Do a google look up on setting the JS option() function.
  • Copy linkTweet thisAlerts:
    @KorApr 11.2011 — hi, all

    i have a problem with appending options from object to select element

    i append options but it shows empty in the select list

    here is my script
    [code=php]
    this[i].option.name=this[i].name;
    [/code]

    [/QUOTE]

    Of course. The option elements do not support the name attribute. Nor should be useful for anything. You must create and append the [B]text[/B]
    <i>
    </i>this[i].option=document.createElement('option');
    [COLOR="Blue"]this[i].option.appendChild(document.createTextNode(this[i].name));[/COLOR]
    this[i].option.value=this[i].value;
    this.menu.appendChild(this[i].option);
    Copy linkTweet thisAlerts:
    @azegurbauthorApr 13.2011 — Thanks all for attention

    Now i understood

    Thanks again
    ×

    Success!

    Help @azegurb 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 5.4,
    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: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

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