Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

my english is'nt very good, so shortly.. Where I have error please in my code?

<script src="js/jquery-1.5.1.min.js" type="text/javascript" /></script>
<script>
$(document).ready(function(){
  $('#2').click(function() {
   $("#element").append(
    "                    <select name='select'>",
        "                            <option>Kč</option>",
        "                            <option>Euro</option>",
    "                   </select>"
      );
    });
});
</script>

<select>
    <option id="1">1</option>
    <option id="2">2</option>
</select>

<div id="element"></div>

Select box show empty :(

Thank you for answer

share|improve this question
1  
Using numbers as ID's isn't advisable.. – Zenith yesterday

2 Answers

When you select an option in a select box .click() is not available.

What you need for that is .change().

http://jsfiddle.net/uEMvr/

HTML

<select id="selectList">
    <option id="1">1</option>
    <option id="2">2</option>
</select>

<div id="element"></div>

JS

$('#selectList').change(function(){ 
    if($(this).val() == "2"){
        $("#element").append(
            "<select name='select'>" +
            "<option>Kč</option>" +
            "<option>Euro</option>" +
            "</select>"
        );
    }
});
share|improve this answer
+1 I didn't notice where the click is fire. – WooCaSh yesterday

I'm not sure but try something that:

  $('#id2').click(function() {
      $("#element").append(
      "<select name='select'>" +
          "<option>Kč</option>" + 
          "<option>Euro</option>"+
        "</select>");
  });

In your code you append to element something that:

<select></select>
<option>Kč</option>
<option>Euro</option>

Also like @Zenith mention you can't use ID which starts from numbers!! So change id's to for example id1 and id2;

share|improve this answer
ok, thank you and how add element using click on button in this code? <button id="add">ADD Element</button> – John Kesy yesterday
button or select list? – WooCaSh yesterday
add this element using select box and click on button "add" – John Kesy 23 hours ago
Did you trying code @user887515 ? – WooCaSh 23 hours ago
Code is perfekt and works, but I can not add button, who add elements instead of select box, but select remains. – John Kesy 23 hours ago
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.