0

Okay, here we go...

I have a page XYZ with a button that's supposed to call my function:

$(function() {


    $('#addgroupicon').on('click', function(event){

        addgroup();

    }); 

});


function addgroup()
{
$('#addgroupdialog').show();
}

// #addgroupdialog and #addgroupicon are within the loaded page, the above code is inside a .js file that I load in the header of the index.html

now I'm loading this page XYZ into a div with the .load() function, but when i click the button nothing happens. I also tried to add onclick='javascript:addgroup();' to the button, with no success.

can anyone help me?

Ben Greene
  • 147
  • 1
  • 15

7 Answers7

4

You're not using "on" in the right way. It should be used like this:

$(document).on("click", "#addgroupicon", function(event){
        addgroup();
}); 

See the following fiddle which mimics your scenario, I think exactly:

http://jsfiddle.net/U4xZj/

To explain why, because I don't think the jquery docs do a very good job at this... Basically, if you think about what you're doing with "on" in the way you were using it:

$('#addgroupicon').on('click', function(event){

You're saying, ok jQuery, I want to create an event handler for the following selector, "addgroupicon". Jquery promptly turns around and goes "great!" #addgroundicon has exactly ZERO items that match it. Go away and stop bothering me. But doing it with $(document) first, jquery goes, "oh, ok. I can certainly create a click event handler for document. Now, if you only want me to raise this event when certain elements caused the click, then please provide a selector, and if the selector matches the source element, then I will call your callback." Make sense?

aquinas
  • 23,318
  • 5
  • 58
  • 81
  • i can see how this is a similar scenario, but for some reason it just won't work in my code – Ben Greene Apr 18 '12 at 06:11
  • i was calling $(function() { in another .js, now that i inserted the code there, it works just fine. obviously $(function() { can only be called once. thanks! – Ben Greene Apr 18 '12 at 06:17
2

Try this. If you're calling the element after the page is loaded, you'll need to use .live().

$(function() {


    $('#addgroupicon').live('click', function(event){

        addgroup();

    }); 

});
sbeliv01
  • 11,550
  • 2
  • 23
  • 24
  • i'm using JQuery 1.7, .live() is deprecated here – Ben Greene Apr 18 '12 at 04:47
  • 1
    that will not work, this refers to the document witch isnt a child node of #addgroupicon `$(this /*or document, if you are planing not to have it around the dom-ready */).on('click', '#addgroupicon', function() {})` – voigtan Apr 18 '12 at 05:16
0
  1. Never seen $(function() {}), try using $(document).ready({})

  2. With $(document).ready({}), your js code will run after page is loaded, if addgroupicon is not there yet, click event will not get bind to it. So if you are adding addgroupicon later, you have to run that code after it has been added.

  3. Why not use simple $('#addgroupicon').click(function(event){})

Milan Halada
  • 1,943
  • 18
  • 28
  • $(function(){}); does the same thing as .ready(); it's the "shorthand" version. – sbeliv01 Apr 18 '12 at 04:53
  • my site consists of 100+ pages i load in divs and about half have this and other buttons and need to call the respective functions, so adding it to each page seperately can't be the smart solution for me. i know that .click is the same as on, just wanted to be clear, that i'm not using the old click but the .on() function. – Ben Greene Apr 18 '12 at 04:54
  • well you can call it in your load() function. Acording to [this](http://api.jquery.com/load/) load can have a callback function that is launched after load completion – Milan Halada Apr 18 '12 at 04:57
  • http://stackoverflow.com/questions/2662778/what-is-the-difference-between-these-jquery-ready-functions – Jashwant Apr 18 '12 at 05:08
0

You can use .bind() method instead of .live().

$(function() {    
    $('#addgroupicon').bind('click', function(event){
        addgroup();
    }); 
});
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
naresh
  • 33
  • 7
0

Try to use live() instead of bind() for button.

freeze
  • 546
  • 6
  • 16
  • http://api.jquery.com/live Dont use `live`, its deprecated, use `on` instead http://api.jquery.com/on/ – Jashwant Apr 18 '12 at 05:14
0

I didnt quite understand your question, but if live() solves your problem and if you are not using it because its deprecated.

Here's the way to go,

$("body").on("click", "#addgroupicon", function(event){
    addgroup();
});

Refer: http://api.jquery.com/on/

Jashwant
  • 28,410
  • 16
  • 70
  • 105
0

curious why you're using .on() Wont this do?

$('#addgroupicon').click(function(){
    $('#addgroupdialog').show();
}); 
ngplayground
  • 20,365
  • 36
  • 94
  • 173