skos
skos

Reputation: 4212

JavaScript Pop up : Invoking in JQuery

In my application I am using a simple JavaScript popup and successfully invoking it this way-

<a href="javascript:popup('Hello World')>Click Me</a>

I was wondering whether it is possible to invoke the same popup on other jQuery events. For instance

    $("#some_button_id").click( function() {

        javascript:popup('Hello World');

    });  

The above method doesn't work. Any other solution?

Upvotes: 1

Views: 249

Answers (3)

mpm
mpm

Reputation: 20154

 $("#some_button_id").click( function() {

        popup('Hello World');

    });  

should work.

EDIT

this will work for sure if the id exit when the event if fired , wether or not it has been created when the the listener was added , it is called delegation :

$(document.body).click( function(e) {
if(e.target.getAttribute("id")=="some_button_id"){
        popup('Hello World');
}
});  

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76870

EDIT - You don't need the javascript: part because you are not attaching javascript inline. But that is not the cause of the error so make sure that you wait until the DOM is ready before attaching an event handler.

$(function(){
    var popup = function(msg){
        alert(msg);
    }
    $("#some_button_id").click( function() {

        popup('Hello World');

    }); 
});

and of course make sure you define popup() somewhere

Upvotes: 1

iMoses
iMoses

Reputation: 4348

If the popup function is defined on your page then you should use

$("#some_button_id").click( function() {
    popup('Hello World');
});

The javascript: prefix is only needed when you use javascript code directly inside your html attributes.

Upvotes: 1

Related Questions