5

I have a page where some html is being dynamically added to the page.

This is the html and javascript that is created:

<div>
    <script type="text/javascript" language="javascript">
        $('#btn').click(function() {
            alert("Hello");
        });
    </script>

    <a id="btn">Button</a>
</div>

Looking in my Firebug console, I get an error that says:

TypeError: $("#btn") is null

jQuery is being loaded on the page initially.

What am I doing wrong here?

Steven
  • 18,761
  • 70
  • 194
  • 296

6 Answers6

10

You have to bind on() (or the events defined within the on() method, to an element that exists in the DOM at the point at which the jQuery was run. Usually this is on $(document).ready() or similar.

Bind to the closest element in which the $('#btn') element will be appended that exists in the DOM on page-load/DOM ready.

Assuming that you're loading the $('#btn') into the #container div (for example), to give:

<div id="container">
    <div>
        <a href="#" id="btn">Button text</a>
    </div>
</div>

Then use:

$('#container').on('click', '#btn', function(){
    alert('Button clicked!');
});
David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • 2
    I don't think that is an issue here.. `TypeError: $("#btn") is null` is not something thrown on dynamic elements. – Selvakumar Arumugam Sep 07 '12 at 21:28
  • @Vega: I hadn't taken proper notice of that error. Hmm. Presumably instead, then, the element is not being selected properly in the first place? – David Thomas Sep 07 '12 at 21:30
3

Use .on to wire up the event to your button. Check this SO answer:

Event binding on dynamically created elements?

$(document).ready(function() {
    $('body').on('click', '#btn', function() {
        alert("Hello");
    });
})

Edit: I added the document ready code, you'll want to make sure you do that.

Fixed.

Community
  • 1
  • 1
Gromer
  • 9,861
  • 4
  • 34
  • 55
1

you are looking for .on here which will bind the click event to dynamically added nodes.

$("#parent_container").on("click", "#btn", function () {
    alert("hello")
})

the docs: http://api.jquery.com/on/

Mark
  • 5,680
  • 2
  • 25
  • 26
0

Try

<div>
     <a id="btn">Button</a>
</div>
 <script>

    $('#btn').on('click', function() {
        alert("Hello");
    });

</script>
jtheman
  • 7,421
  • 3
  • 28
  • 39
0

The problem in your code is that you are attaching the event to the button before the button is being created.

Correct version is:

  <div>
       <a id="btn">Button</a>
       <script type="text/javascript" language="javascript">
           $('#btn').click(function() {
              alert("Hello");
           });
       </script>            
  </div>

This should do the job.

Catalin MUNTEANU
  • 5,618
  • 2
  • 35
  • 43
0

Use the .live() function of jQuery

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
Ethan Kawkji
  • 217
  • 2
  • 9
  • As a note about .live() from the jQuery docs: "As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live()." – Gromer Sep 14 '12 at 15:14