Eric P wrote:

> Hi,
>
> I'm fairly new to jQuery (been using a few months now).  Binding event 
> handlers to HTML objects via jQuery is awesome,
> but I find myself struggling to find a solid (I.e., best practice) method for 
> getting numerous arguments to the event
> handler that are pertinent to the object that triggered the event.
>
>
> For example, old method:
> <script>
> function buy(item, color, price) { ... }
> </script>
> ...
> <img src="item01.jpg /><a href="javascript:buy('123-ABC','red',9.99)>Buy 
> Me!</a><br />

The data in the href is put there by your server, so why no put it in
a real url?

  <img src="item01.jpg /><a href="http://....com/blah?item=123-
ABC&color=red&price=9.99" onclick="buy(this);">Buy Me!</a><br />

Now the page can be made to work easily without javascript. If you are
creating a shopping cart, then delegate the click listener to a parent
element that compiles the shopping list and cancels navigation (which
is what I guess you are doing now).  That way you only have one
listener.

If you add the listener dynamically, pass event and use that to find
the element that was clicked and the data in its href.

[...]

> So in the jQuery method, what is the best way to make the multiple arguments 
> (that were previously in the inline HTML/JS
> function call) available to the event handling function?

Put them in the href attribute as a genuine URL, then grab them when
the event fires.  You can also use the class attribute.


--
Rob

Reply via email to