views:

67

answers:

5

i got this url

<a class="remove_item' rel="4" onclick="javascript:jQuery(#blablabla)" href="javascript:;' >remove</a> 

i'm using this simple find and replace

item.find('a.remove_class').attr({
title:'hello',
click:'hello();'
} );

but it seem it's not working. i still not able to replace javascript:jQuery(#blablabla) with another function.

+2  A: 

To set onClick you should use something like this $("a").attr("onclick", js);

where js is a string containing your javascript code.

Sorantis
do you have any reference link that i can learn it more ? i know i can google it. but it better if you choose it for me. i have do the google and confuse
justjoe
http://docs.jquery.com/Main_Page - all API is very well documented, with examples. Also http://www.heinmaas.com/all-the-jquery-resources-youll-ever-need/ here is the collection of useful jQuery stuff you'll need.
Sorantis
Be careful. You can set the `onclick` attribute like that, but if you try to get it using `attr("onclick")` you won't get the string. For me it's a bug, but it might have some reason behind this behavior.
BrunoLM
+2  A: 

Why not just do this.

<script type="text/javascript">
    $(document).ready(function() {
        $("a.remove_item").click(function() {
            alert("You have clicked my <a>!!");
            //do other stuff
        });
    });
</script>
Randall Kwiatkowski
+3  A: 

Try attaching the event handler using the below code snippet in your page body:

<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $('.remove_item').click(hello);
    });
</script>
Floyd Pink
+2  A: 

jQuery has no built-in way to assign event handlers in that way. Use the DOM0 method to set the onclick handler instead:

item.find('a.remove_class').each(function() {
    this.onclick = function() {
        alert("Clicked!");
    };
});

Also, don't put javascript: in you event handler attributes. It's incorrect and only works by coincidence.

Tim Down
+1  A: 

The attr doesn't recognises the onclick attribute, get the html element (.get(0)) and use "raw" functions to extract the onclick attribute.

On Firefox the following works:

$("element").get(0).getAttribute("onclick")

You can remove by using

$("element").get(0).removeAttribute("onclick")

or setting a empty string

$("element").get(0).setAttribute("onclick", "")
BrunoLM