1

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.

3
  • Please post the code you've tried so far. Commented Apr 10, 2013 at 10:17
  • Why don't you keep the function same but change the input parameters depending on the situation ? Commented Apr 10, 2013 at 10:20
  • You're probably better off (re)binding to it using the $('link_element_id').click(function(){ //code here }) Commented Apr 10, 2013 at 10:20

3 Answers 3

2

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");
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

http://jsfiddle.net/neX7W/

Comments

1

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>

3 Comments

Using the id attribute to store the value is probably a really bad idea, since they have to be unique.
It is an example, i know you can do it in better ways, see the note. I'll keep its easy so he understands then he can do what he likes.
I'm not sure I'd agree with the assessment that this is just "worse", I think I'd go as far as to say that it's just plain wrong. If you know that it can be done using a data-* attribute, an example of how that would be achieved would be considerably better (I'd even upvote that).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.