javascript - Attach data to HTML nodes -
this question has been asked before, of answers revolve around html5.
my doctype is:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
i need attach data (phone dialling code) option values of select:
<select id="id_creditcardcountry"> <option value="ax">aland islands - ax</option> <option value="al">albania - al</option> <option value="dz">algeria - dz</option><!--e.g. attach dial code "(+213)" --> <option value="as">american samoa - as</option> <option value="ad">andorra - ad</option> <option value="ai">anguilla - ai</option> etc
there seem numerous ways accomplish this. need find best 1 (by best mean cross browser , efficient).
- the jquery "data" function http://docs.jquery.com/core/data -- if best method, can give guidance of how use in instance?
- set data based on element id - involves having give id every option in select.
- further suggestions?
you use:
$("#id_creditcardcountry option[value='ax']").data("dial-code", "+213");
if have js array of countries , dial codes loop through them all:
for(var = 1; <= countryarray.length; i++){ $("#id_creditcardcountry option[value='"+countryarray[i]+"']").data("dial-code", ""+dialcodearray[i]+""); }
however, may consider doing server side if you're outputting list of countries. example:
<option value="ax" data-dial="+213">aland islands - ax</option>
Comments
Post a Comment