1

I've searched through stackoverflow as well as the rest of the web without finding a clear answer for what I am trying to do. I have the following HTML code:

<a id="orderPadReviewSubmitBtn" href="#" class="portlet-lightGrey-button  float-right"><span>Submit order</span></a>

which is loaded via JavaScript from a button click. I am attempting to use delegate as the jQuery on the site is 1.4.2, so no "on()" is available.

The code I am trying is this:

$("a[id=orderPadReviewSubmitBtn]").delegate("span","load", function(){
$("#orderPadReviewSubmitBtn").mousedown(function(){alert("Order Submitted");}); });

I have also tried:

$("#orderPadReviewSubmitBtn").delegate("span","load", function(){
$("#orderPadReviewSubmitBtn").mousedown(function(){alert("Order Submitted");}); })

These work if loaded once the above button is loaded, but not if I run the jQuery before the button is loaded.

My ultimate goal is to be able to run some other function once this button loads and someone clicks on it.

I have a feeling that there is something simple I am missing, and your help in finding the issues would be greatly appreciated.

Gromer
  • 9,305
  • 4
  • 32
  • 54
Andrew
  • 15
  • 3
  • Everything I have read said to stay away from .live() and use .delegate() for this. – Andrew Oct 18 '12 at 21:04
  • Actually delegate was added in 1.4.2 so you should use that instead. Also whats the reason for binding the load event? – wirey00 Oct 18 '12 at 21:05
  • Yeah, you should be binding the click event. – Reinstate Monica Cellio Oct 18 '12 at 21:05
  • I wasn't able to get delegate to work, but was able to get the desired result using live (I know, I said myself that I shouldn't), but it worked. The final code was $("#orderPadReviewSubmitBtn").live("click", function(){alert("Order Submitted");}); – Andrew Oct 18 '12 at 22:10

2 Answers2

1

You don't need to use the load event handler

// bind event handler to parent that exists at the time dom is ready
// it will then listen for click event from element with id=orderPadReviewSubmitBtn
$(function(){ // <-- put your code inside a document.ready function
    $('body').delegate('#orderPadReviewSubmitBtn','click',function(){
        alert("Order Submitted");
    });   
});

Replacing body with the closest static parent element

I think you don't understand how delegation works - That's why you think you need the load event. How it works is you bind it to a parent element that already exists and is static(meaning it won't change) - It will then listen for the events you specify, as they bubble up from the specific element/elements you use chose(in this case your element with id=orderPadReviewSubmitBtn)

wirey00
  • 32,479
  • 7
  • 49
  • 64
  • That's still a load event handler :) – A.M.K Oct 18 '12 at 21:14
  • @A.M.K What's a load event handler? – wirey00 Oct 18 '12 at 21:14
  • `$(function(){` is actually just a wrapper function for `window.onload`. – A.M.K Oct 18 '12 at 21:17
  • Actually - `$(function() == $(document).ready(function()` which is not the same as window.onload. Read more about it here http://stackoverflow.com/a/3698214/1385672 – wirey00 Oct 18 '12 at 21:18
  • I stand corrected, partially. As you can see [here](http://james.padolsey.com/jquery/#v=1.7.2&fn=jQuery.bindReady) it calls `DOMContentLoaded` and falls back to `onload`. – A.M.K Oct 18 '12 at 21:19
0

You can use .live(), it isn't depreciated in 1.4, your code would end up being this:

$("a[id=orderPadReviewSubmitBtn]").live("mousedown", function() {
    alert("Order Submitted");
});​

Demo: http://jsfiddle.net/SO_AMK/d2SYE/

And, as you mentioned, if you wanted to call another function you'd use this:

$("a[id=orderPadReviewSubmitBtn]").live("mousedown", newFunc);

function newFunc() {
    alert("Order Submitted");
}​

Demo: http://jsfiddle.net/SO_AMK/YwNyP/

A.M.K
  • 15,539
  • 3
  • 27
  • 61
  • Looks to me like a misunderstanding on his part - not being rude but he's obviously not that familiar with jQuery. I get what you mean though. – Reinstate Monica Cellio Oct 18 '12 at 21:06
  • Yeah, I spotted that a moment ago myself. Facepalm :) – Reinstate Monica Cellio Oct 18 '12 at 21:10
  • Thanks Everyone for your help. @Archer, you are correct. I am a JS guy who is now trying to do everything I can in jQuery as it is much better in many ways. Now I more clearly understand how delegate and live works. – Andrew Oct 18 '12 at 22:07
  • 1
    @Andrew Stick with it mate - it's AWESOME when you start getting into it :D – Reinstate Monica Cellio Oct 18 '12 at 22:12
  • @Archer +1 Definitely, but, there's one downside, you end up loving it so much you never use vanilla JS again and then when you have to you can barely stumble through it! (That's a slight exaggeration but it's close, if I try vanilla I find myself using JQ methods!) – A.M.K Oct 18 '12 at 22:15
  • @A.M.K. So very true...I actually get a little depressed when I have to use JS for something where I know jQuery would have been much easier/cleaner. Fortunately, most of my clients have jQuery so I don't have to be sad very often. – Andrew Oct 19 '12 at 00:14