0

I want to add div structure everytime I click the button to add an address using jQuery

<div id="container">
      <div class="address">
           <div class="input-reg rb-item input-group">
                 <span class="input-group-addon">Address </span>
                 <input type="text" class="form-control" placeholder="Insert text here">
           </div>
           <div align="center"><iframe class="map-img" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&amp;ie=UTF8&amp;ll=37.0625,-95.677068&amp;spn=56.506174,79.013672&amp;t=m&amp;z=4&amp;output=embed"></iframe></div>
       </div>
</div>
<div align="center" style="margin-top: 10px"><button id="btnAddAddress" class="btn btn-warning btn-md" type="button">Add Address</button></div>

and this is what I've tried but not working

<script type="text/javascript">
    $("#btnAddAddress").click(function () {
      $("#container").append('<div class="address"><div class="input-reg rb-item input-group"><span class="input-group-addon">Address </span><input type="text" class="form-control" placeholder="Insert text here"></div><div align="center"><iframe class="map-img" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&amp;ie=UTF8&amp;ll=37.0625,-95.677068&amp;spn=56.506174,79.013672&amp;t=m&amp;z=4&amp;output=embed"></iframe></div></div>');
    });
</script>
8
  • Have you loaded jquery? What error message do you get? Commented Sep 29, 2014 at 11:59
  • Not working means?? what it does? Commented Sep 29, 2014 at 11:59
  • I don't get error msg I'm using Brackets source editor Commented Sep 29, 2014 at 11:59
  • 2
    Have you got something like this somehwere in your code: <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>? Commented Sep 29, 2014 at 12:01
  • your code seems to be fine for me jsfiddle.net/mrr643f1 Commented Sep 29, 2014 at 12:01

4 Answers 4

4

You have to enclose your jquery code inside $(document).ready() block as shown :-

$(document).ready(function(){
  //Jquery code here
});

OR

$(document).on('click', '#btnAddAddress', function() 
{...});

and lastly don't forget to add jquery library file on your page.

Note :- Second answer should normally be used in case when we dynamically add html to the DOM otherwise use first answer.

Working Demo

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

9 Comments

@Azrael..jquery code should be inside document.ready block..plz read the docs carefully...
@user3667305...just enclose jquery code inside document.ready() as shown here.
Actually he has a point.. Since .click() is being used here and not .on('click'...) .. then the doc needs to be loaded in order to FIND the element on which to add an event listener on..
@user3667305 You should either do what Kartikeya suggested or.. $(document).on('click', '#btnAddAddress', function() {...} .. or both..
@webkit $(document).on('click', '#btnAddAddress' is bad idea in case of perfomance.
|
1

Your code is working fine here. Also I have simplified your code like below.

$("#btnAddAddress").click(function () {
     $("#container").append($(".address").html());
});

Comments

1

The code you provided will work with out any modifications, if the js code is written after the button. i.e.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
      <div class="address">
           <div class="input-reg rb-item input-group">
                 <span class="input-group-addon">Address </span>
                 <input type="text" class="form-control" placeholder="Insert text here">
           </div>
           <div align="center"><iframe class="map-img" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&amp;ie=UTF8&amp;ll=37.0625,-95.677068&amp;spn=56.506174,79.013672&amp;t=m&amp;z=4&amp;output=embed"></iframe></div>
       </div>
</div>
<div align="center" style="margin-top: 10px"><button id="btnAddAddress" class="btn btn-warning btn-md" type="button">Add Address</button></div>

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
    $("#btnAddAddress").click(function () {
      $("#container").append('<div class="address"><div class="input-reg rb-item input-group"><span class="input-group-addon">Address </span><input type="text" class="form-control" placeholder="Insert text here"></div><div align="center"><iframe class="map-img" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&amp;ie=UTF8&amp;ll=37.0625,-95.677068&amp;spn=56.506174,79.013672&amp;t=m&amp;z=4&amp;output=embed"></iframe></div></div>');
    });
</script>

Make sure that the button and jquery is loaded before your code is executed. The best option is provided by "Kartikeya".

Comments

0

You need to add "return false;" at the end of click function.

$("#btnAddAddress").click(function () {
   $("#container").append('<div class="address"><div class="input-reg rb-item input-group"><span class="input-group-addon">Address </span><input type="text" class="form-control" placeholder="Insert text here"></div><div align="center"><iframe class="map-img" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&amp;ie=UTF8&amp;ll=37.0625,-95.677068&amp;spn=56.506174,79.013672&amp;t=m&amp;z=4&amp;output=embed"></iframe></div></div>');
   return false;
});

JSFiddle Example

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.