javascript - Populating a selectbox from html content split by commas -


i writing code form select box in has populated content of specific html part. managed id on specific part (it gets created automatically cms) can call part javascript.

problem cant figure out how content split commas "red, blue, yellow" , split select box options.

here part of javascript code have:

    function offerte(){     var kleuren = document.getelementbyid('product_kleuren');   // here has come code populate select box     } 

and html part:

    <div><span class="extra_fields_name">kleuren</span>:     <span id="product_kleuren" class="extra_fields_value">naturel, helder, zwart</span></div>      <label class="offerte_lbl">kleur: </label><select id='kleuren'  class='offerte_input'></select><br> 

i know not much, i'm still learning javascript cant figure out how content of specific html part split commas , seperate them values populate selectbox with.

using pure js, can use .split(",") put split values in array, iterate through array creating option elements:

<select id="someselect"></select> 

js

function offerte(){     var kleuren = document.getelementbyid('product_kleuren');     var parts = kleuren.innerhtml.split(",");      (var = 0; < parts.length; i++) {         var option = document.createelement("option");         option.text = parts[i];         option.value = i;         document.getelementbyid("someselect").appendchild(option);     } } 

demo: http://jsfiddle.net/fvdph/


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -