jQuery event.PreventDefault doesn't work in user defined function


Mickey

I am creating a function that will receive a URL and then display that URL in a lightbox. I'm having trouble with event.preventDefault() in a function, it says it's not a function in the error console.

I've tried passing the event to the function explicitly, but that only runs the function on page load and not on click

Here is my code:

    // Function to display widget
    function displaySportsWidget(event, iframeURL) {
        // Prevent Default Event Handler
        event.preventDefault();

        // Build iFrame HTML
        var iframe = '<iframe src="' +  iframeURL + '" ></iframe>';         
        var html = iframe;

         // Inject HTML to Generate Widget
        $('.leagues-wrapper').html(html);

        // Display Overlay
        $('.leagues-overlay').css('display', 'block');
    };

    // Event handlers. Pass iFrame URL into function depending on link clicked
    $('.leagues-link.league-table').on('click', displaySportsWidget('http://www.example01.com'));
    $('.leagues-link.league-form').on('click', displaySportsWidget( 'http://www.example02.com'));   
TJ crowd

code

$('.leagues-link.league-table').on('click', displaySportsWidget('http://www.example01.com'));

Call displaySportsWidget and pass its return value into in exactly the same way as onyou foo(bar()) call bar and pass its return value into foo.

If you want to hook up an event handler, don't call it, just reference it. In your case you can do this:

$('.leagues-link.league-table').on('click', function(e) {
    displaySportsWidget(e, 'http://www.example01.com');
});

Or, if you prefer to change the order of the following parameters displaySportsWidget:

function displaySportsWidget(iFrameURL, event) {
    // ...
}

...then you can use Function#bind:

$('.leagues-link.league-table').on('click', displaySportsWidget.bind(null, 'http://www.example01.com'));

Function#bindCreate a new function that, when called, calls the original function with the given thisvalue (in our case we don't need any specific function , so I'm passing it null) and bindall the arguments you give, then the given of any parameter binding functions. So we will get the URL (from the bindcall) and the event object (from the time the handler was called).

Function#bindNot really old browsers (eg IE8). If you need to support them, jQuery does something $.proxylike this:

$('.leagues-link.league-table').on('click', $.proxy(displaySportsWidget, null, 'http://www.example01.com'));

Related


jQuery event.PreventDefault doesn't work in user defined function

Mickey I am creating a function that will receive a URL and then display that URL in a lightbox. I'm having trouble with event.preventDefault() in a function, it says it's not a function in the error console. I've tried passing the event to the function explic

event.preventDefault() function doesn't work in IE

sv_in : Here is my JavaScript (mootools) code: $('orderNowForm').addEvent('submit', function (event) { event.preventDefault(); allFilled = false; $$(".required").each(function (inp) { if (inp.getValue() != '') { allFilled = true

event.preventDefault() function doesn't work in IE

sv_in : Here is my JavaScript (mootools) code: $('orderNowForm').addEvent('submit', function (event) { event.preventDefault(); allFilled = false; $$(".required").each(function (inp) { if (inp.getValue() != '') { allFilled = true

event.PreventDefault(); doesn't work in nested onClick function

Abdullah Norman I am using an ajax function to submit my data to the controller, the function returns the result perfectly, but the problem is that the ajax is posting the data multiple times when the click event is fired. Note that my function structure is in

event.PreventDefault(); doesn't work in nested onClick function

Abdullah Norman I am using an ajax function to submit my data to the controller, the function returns the result perfectly, but the problem is that the ajax is posting the data multiple times when the click event is fired. Note that my function structure is in

event.preventDefault() function doesn't work in IE

sv_in : Here is my JavaScript (mootools) code: $('orderNowForm').addEvent('submit', function (event) { event.preventDefault(); allFilled = false; $$(".required").each(function (inp) { if (inp.getValue() != '') { allFilled = true

jQuery .click() event.preventDefault() doesn't work

Richard King: I'm building an extensible web application by putting scripts into a preset directory. The script needs to read the file header before going to the page, so I need to use .preventDefault() when I click the link and run some JScript first. Unfortu

jQuery .click() event.preventDefault() doesn't work

Richard King: I'm building an extensible web application by putting scripts into a preset directory. The script needs to read the file header before going to the page, so I need to use .preventDefault() when I click the link and run some JScript first. Unfortu

jQuery .click() event.preventDefault() doesn't work

Richard King I'm building an extensible web application by putting scripts into a preset directory. The script needs to read the file header before going to the page, so I need to use .preventDefault() when I click the link and run some JScript first. Unfortun

jQuery event.preventDefault() doesn't work unless debugging

metal encoder I am trying to use this method in order to convert GET parameters to POST. I also want the anchors to be handled correctly even when the middle mouse button is clicked. This is how the event handler is bound: $(document).ready(function () { $

jQuery .click() event.preventDefault() doesn't work

Richard King: I'm building an extensible web application by putting scripts into a preset directory. The script needs to read the file header before going to the page, so I need to use .preventDefault() when I click the link and run some JScript first. Unfortu

jQuery preventDefault() doesn't work with .on()

Mr Gandy Yes, I've read many other articles with the same problem, but I don't understand how to fix this. I have created a bookmark button that needs to change the icon when clicked. my jQuery: jQuery(document).ready( function() { var bm = jQuery(".u

jQuery preventDefault() doesn't work with .on()

Mr Gandy Yes, I've read many other articles with the same problem, but I don't understand how to fix this. I have created a bookmark button that needs to change the icon when clicked. my jQuery: jQuery(document).ready( function() { var bm = jQuery(".u

jQuery preventDefault() doesn't work with .on()

Mr Gandy Yes, I've read many other articles with the same problem, but I don't understand how to fix this. I have created a bookmark button that needs to change the icon when clicked. my jQuery: jQuery(document).ready( function() { var bm = jQuery(".u

jQuery preventDefault() doesn't work with .on()

Mr Gandy Yes, I've read many other articles with the same problem, but I don't understand how to fix this. I have created a bookmark button that needs to change the icon when clicked. my jQuery: jQuery(document).ready( function() { var bm = jQuery(".u

jQuery preventDefault() doesn't work with .on()

Mr Gandy Yes, I've read many other articles with the same problem, but I don't understand how to fix this. I have created a bookmark button that needs to change the icon when clicked. my jQuery: jQuery(document).ready( function() { var bm = jQuery(".u

event.preventDefault() on keydown doesn't work

Stave Tried to capture keypresses on the Bootstrap tab panel menu, but ignored the preventDefault() placed on the tab's keypress handler, it just bubbled up. document.onkeydown = function(e) { console.log("document catched the keydown event"); }; $('body >

event.preventdefault doesn't work in Firefox

in Nakyo I'm trying to make part of the text in the Input read-only to achieve what I found this code but doesn't work in FIrefox, works fine in chrome. Found out that event.preventDefault was causing the problem. Tried returning false, that didn't work either

event.preventDefault() on keydown doesn't work

Stave Tried to capture keypresses on the Bootstrap tab panel menu, but ignored the preventDefault() placed on the tab's keypress handler, it just bubbled up. document.onkeydown = function(e) { console.log("document catched the keydown event"); }; $('body >

event.preventDefault() on keydown doesn't work

Stave Tried to capture keypresses on the Bootstrap tab panel menu, but ignored the preventDefault() placed on the tab's keypress handler, it just bubbled up. document.onkeydown = function(e) { console.log("document catched the keydown event"); }; $('body >

React doesn't work for preventDefault() on onCopy event

Sviat Kuzhelev I'm trying to figure out how to make the clipboard event return falseon the onCopy event . I use test onCopyhandlers and e.preventDefault()methods. But the text is copied to the buffer without a hitch! What am I missing? Thank you in advance. im

event.preventDefault doesn't work in React

Joshua Rajandiran I followed a tutorial and they used them on event.preventDefault()a Savebutton and saved the form to the state. I haven't really written inputthe label yet, but so far I've added a "save" button, which is kind of like reloading the page when

event.preventDefault() doesn't work on click

Dai Du I want to stop event click. I tried using event.preventDefault()"ul li a" to stop the event. <ul> <li><a href="#">Click here</a></li> </ul> <script type="text/javascript"> $(document).ready(function() { $(document).on('click','ul li a', functi

event.preventdefault doesn't work in Firefox

in Nakyo I'm trying to make part of the text in the Input read-only to achieve what I found this code but doesn't work in FIrefox, works fine in chrome. Found out that event.preventDefault was causing the problem. Tried returning false, that didn't work either

event.preventdefault doesn't work in Firefox

Fisolotian I am using the jQuery below to toggle the hidden div. It works everywhere except Firefox, I know the error is (ReferenceError: event not defined), but I'm not sure where to define the event, so if someone could help that would be great. Thanks in ad

preventDefault() doesn't work on change event

SlimJim Any ideas why preventDefault doesn't work? Here is the code below. . . Tks! <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> $(document).ready(function() { $("#text1").change(function(e) {

.unbind() doesn't work with event.preventDefault()

Justin Medas I have a responsive Javascript file that works successfully to unbind basic jQuery functionality. When the browser is set to tablet, the navigation is toggled based on click, so I disabled linking to any li element with ul. $('.main-navigation .me