Ashok
Ashok

Reputation: 128

Change "onclick" call dynamically using jquery

I have javascript call for an image during onclick event.

onclick="removeFeetype(5);"

I need to change that to onclick="removeFeetype(4); during some other call using the attribute ID by jQuery. Which means I need to change the function call from 5 to 4.

Please I need your help. Thanks in advance.

I tried using the following code to change the "onclick" call method. But it is not changed on my html code for that particular element with the ID "removeButton".

------------------------FIRST-------------------------------

var changeval = 4;
var onclickval = "removeFeetype("+changeval+");";
$('#removeButton').attr("onclick", onclickval);

------------------------SECOND-----------------------------------------

var changeval = 4;
var next = "removeFeetype(" + changeval + ");";
var newclick_next = eval("(function(){"+next+"});");
$('#removeButton').attr('onclick','').unbind().click(newclick_next);

Actually my task is,

i have sequence for the rows with the ID

id="removeButton1" onclick="removeFeetype(1);"
id="removeButton2" onclick="removeFeetype(2);"
id="removeButton3" onclick="removeFeetype(3);"
id="removeButton4" onclick="removeFeetype(4);"

.....

If i delete the removeButton2 using the function removeFeetype(2).

then i need the change remaining to

ID = removeButton3 to removeButton2 and so on and
ONCLICK = removeFeetype(3) to removeFeetype(2) and so on

I can change the ID removeButton3 to removeButton2 and so on.

But i can't change the onclick method call from "removeFeetype(3);" to "removeFeetype(2);"

Hope now it is clear. Please help me to fix it.

Upvotes: 1

Views: 2602

Answers (3)

s.lenders
s.lenders

Reputation: 1149

Lets use jQuery overall

$(document).ready(function(){
    $(".Fee").click(function(){
       removeFeetype($(this).attr('data-value'));
       $(this).attr('data-value',$(this).attr('data-value') - 1);
    }); 
});

I'll expect your link to be

<div class="Fee" data-value="5">Link</div>

Upvotes: 1

Stefan
Stefan

Reputation: 1106

You can try to change the onclick attribute value like this:

$("#link").attr("onclick", "removeFeetype(4);");

http://jsfiddle.net/neX7W/

Upvotes: 1

Adil
Adil

Reputation: 148110

You can use data() attributes to pass value to javascript, you will need to pass the source object using this.

<input type="text" data-someAtt="1" onclick="removeFeetype(this)" />

function removeFeetype(obj)
{
    $(obj).data("someAtt");
}

Upvotes: 2

Related Questions