0

On clicking the link below, what I want is to change the onclick function like onclick="linkSubCategory(1, \'people\', \'1\', \'peopleLink\');" that is changing the third attribute to 1 from 0.

<a href="#" id="peopleLink" onclick="linkSubCategory(1, \'people\', \'0\', \'peopleLink\');" style="display: inline-block;"><img src="images/icons/group.png"></a>

For my above requirement I am using the code below but it is not working. Kindly suggest me a way to achieve my requirement.

function linkSubCategory(divID, divName, chkVal, linkID) {
$("#"+linkID).removeAttr("onclick");
        $("#"+linkID).attr("onclick", "linkSubCategory("+divID+", '"+divName+"', '1', '"+ linkID +"')");}
1
  • I think it would me more proper to store divId , divName, and chkval on the link using data attributes than passing them to the onclick function. Then you could do $('#peoplelink').click(function(){divId=$(this).data('divId')... and set the chkval accordingly. This way you don't neeed to rewrite the handler , just the data attributes. Commented Aug 8, 2013 at 11:40

1 Answer 1

1

The way I see things:

<a href="#" id="peopleLink" data-divid="1" data-divname="people" data-chkval="0" class="linkSubCategory" ></a>

jQuery

$('.linkSubCategory').click(function(){
   linkSubCategory($(this).data('divid'), $(this).data('divname'), $(this).data('chkval'), this.id);
   $(this).data('chkval',1);
});

This way you don't need to change the event handler. Cause imo , changing the handler means you want to radically change the way it works, but here you just need to change a parameter.

Sign up to request clarification or add additional context in comments.

Comments

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.