0
<select> <script type="text/javascript"> $.get( '/api/load/maron_online/470', function(data){ var mydata = new Array(); var i = 0; // индекс массива материалов $('name', data).each(function(){ if($(this).text()=='MATERIAL_ID') mydata[i++] = new Array(); // массив материалов mydata[i-1][$(this).text()] = $(this).next().text(); }); var htm = ''; for(i in mydata) htm += "<option value=\"" + mydata[i]['TITLE'] + "\">" + mydata[i]['TITLE'] + "</option>"; $('#real').html(htm); }, 'xml' ); </script> </select> 

I want to make a select option but the select tags that I added before and after javascript code did not work. I want the result to be

<select><option value=n>n</option><option value=x>x</value></select> 
2
  • Did not work? Any errors? Is the mark-up wrong? Commented Jun 1, 2013 at 21:05
  • this is what i get: link Commented Jun 1, 2013 at 21:08

3 Answers 3

2

Where is the select tag

 var htm = '<select>'; for(i in mydata) htm += "<option value=\"" + mydata[i]['TITLE'] + "\">" + mydata[i]['TITLE'] + "</option>"; htm += '</select>'; $('#real').html(htm); // make sure you have this element on the page 
Sign up to request clarification or add additional context in comments.

Comments

0
var $sel= $('selectId'); var $opt = $('<option></option>'); for (...) { // set the value and text $opt.val(value); $opt.text(text); // add to the select $sel.append($opt); } 

EDIT

I just noticed your script tag is inside your select tag. Your script tags should be at the bottom of your body tag.

Comments

0

Please see the following fiddle. It might help.It creates a select tag with options as you specified.

http://jsfiddle.net/itz2k13/LtfFz/

jQuery(document).ready(function(){ var options = ""; for (i = 0; i < 10; i++){ options += "<option value = "+i+">"+i+"</option>"; } jQuery("#select_option").append(options); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.