0

I have a dynamically generated list of hyperlinks and i'm using jquery to bind the click events, everything is working fine, just one thing i am unable to do is to changes its text

 **this.value = s;**

This is what I was trying to do without any success.

My full code:

$(document).ready(function () {
    $('[id*="lnkStatus_"]').bind('click', SaveRequirmentStatus);
});

function SaveRequirmentStatus(event) {
    var itemID = $(event.currentTarget).attr('id');
    var intProjectId = $('[id$="hdnProjectId"]').val();
    var idRequirment = itemID.split('_')[1];
    var idRequirementPhase = itemID.split('_')[2];
    var idPhaseStatus = $(event.currentTarget).val();
    if (intProjectId != '0' && idRequirment != '0' && idRequirementPhase != '0') {
        $.getJSON('handler/RequirementLifecycleHandler.ashx?     FuncName=SaveRequirment&idRequirment=' + idRequirment + "&idRequirementPhase=" + idRequirementPhase + "&idProject=" + intProjectId + "&idPhaseStatus=" + idPhaseStatus, function (ValueStatus) {
            var s = ValueStatus;
            alert(this);
            this.value = s;
        });
    }
}    

2 Answers 2

1

this in the context that you are using it does not refer to the link, so save a reference to it outside of the inner function and use that. Also, a link does not have a value, you can set the text using the jQuery text function.

Changing your code to this should do what you want:

function SaveRequirmentStatus(event) {
    var $this = this; // save reference to the clicked link
    var itemID=$(event.currentTarget).attr('id');
    var intProjectId=$('[id$="hdnProjectId"]').val();
    var idRequirment=itemID.split('_')[1];
    var idRequirementPhase=itemID.split('_')[2];
    var idPhaseStatus = $(event.currentTarget).val();
    if (intProjectId != '0' && idRequirment != '0' && idRequirementPhase != '0') {
        $.getJSON('handler/RequirementLifecycleHandler.ashx?FuncName=SaveRequirment&idRequirment=' + idRequirment + "&idRequirementPhase=" + idRequirementPhase + "&idProject=" + intProjectId + "&idPhaseStatus=" + idPhaseStatus, function(ValueStatus) {
            $this.text(ValueStatus); // set the text of the link to ValueStatus
        });
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Is there an error or does it simply not do anything? If you add a breakpoint at $this.text(ValueStatus); does it ever get hit?
@user2553774 You should at least use console.log or firebug or any other tool to debug the code. Give out more info.
@RichardDalton I believe OP copied var $this = this; instead of adding the link code there.
I have put alert before and after this line, both alert firing but text is not being changed
@user2553774 Can you post the HTML that is clicked to fire this event?
0

This should do

$(function() {
$('[id*="lnkStatus_"]').bind('click', SaveRequirmentStatus); 

});
function SaveRequirmentStatus(event) {
 $(this).text(ValueStatus);
}

1 Comment

I have put alert before and after '$(this).text(ValueStatus);', both alert firing but text is not being changed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.