0

It may sound trivial but I am unable to do it.

Here is the simple code : what's wrong I am doing here ?

Is it like I have misunderstood the function? If yes, please correct me.

 <html>
  <head>
   <script type="text/javascript" src="jquery.js"></script>                             
    <script type="text/javascript">
      $(document).ready(function(){
       $(".button").click(function(){
        jQuery('#id').append('<select></select>');
       });     
      });
   </script>
  </head>

  <body >
    <input type="submit" class="button" id="id" value="submit"/>
  </body>

5
  • where do you expect the select to be placed? you have to provide a valid selector for an element in the page Commented Nov 22, 2012 at 6:57
  • where is an element with id on your page? Commented Nov 22, 2012 at 6:58
  • What do you means by #id in the code.Provide valid id to append dropdown Commented Nov 22, 2012 at 6:59
  • 1
    Your code is not working because you are trying to insert the select in your input element. Create a new <div id="select-list" container and append your select to it. Commented Nov 22, 2012 at 7:23
  • @alex yes that was the actual problem. Thanks Commented Nov 22, 2012 at 7:24

4 Answers 4

1

considering your own code you can simply write

$('#id').after('<select></select>');

generally append works with a container since #id is associated with a button so it will not work. use div/span if you still want to use append, but if you don't want to .after() works fine.

Sign up to request clarification or add additional context in comments.

Comments

1

you have no container/element in your body that will hold the newly added select, add a container in your html like this with id="id"

<body >
     <div id="id">
     </div>
    <input type="submit" class="button" value="submit"/>
</body>

DEMO

Comments

1

Does #id exists in the document?

Do you want to add <option> to <select> ?

$(document).ready(function() {
    $(".button").click(function() {
        jQuery('body').append('<select><option value="1">One</option><option value="2">Two</option></select>');
    });
});​

Fiddle - http://jsfiddle.net/XVmuZ/

If you want to add it to #id then make sure it exists (like in rahul's answer)

Fiddle - http://jsfiddle.net/XVmuZ/1/

Comments

0

fisrt thing don't use submit button because it will postback your page so jquery will not work. try this code

<html>
  <head>
   <script type="text/javascript" src="jquery.js"></script>                             
    <script type="text/javascript">
      $(document).ready(function(){
       $(".button").click(function(){
        $('#ddl').append('<select><option>abc</option><option>def</option></select>');
       });     
      });
   </script>
  </head>

  <body >
    <input type="button" class="button" id="id" value="submit"/>
    <div id="ddl">
    </div>
  </body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.