AbhiJA
AbhiJA

Reputation: 341

how to change dynamically adding element text using id?

I am dynamically adding <a></a> element.

<div id="divbindProjectlist" class="col-sm-3">
    <a class="list-group-item active left-menu-head">Projects List</a>
    <a href="#" class="list-group-item projectList"><span class="project-name">Project 1</span><input type="hidden" class="hidprojectId" value="0.34084661019770524"></a>
    <a href="#" class="list-group-item projectList"><span class="project-name">Project 2</span><input type="hidden" class="hidprojectId" value="0.6971764238216627"></a>
    <a href="#" class="list-group-item projectList"><span class="project-name">Project 3</span><input type="hidden" class="hidprojectId" value="0.29109692149538624"></a>
</div>

Using below jQuery,

if (json.idPartnerProject == "0") {
    $("#divbindProjectlist").append('<a href="#" class="list-group-item projectList" >' 
      + json.ProjectName + '<input type="hidden" class="hidprojectId" value="' 
      + json.idPartnerProject + '" /></a>');
}

After I am trying to update the existing Project name with respect to idPartnerProject id.

if (obj.idPartnerProject == pro.idPartnerProject) {
 //pro.ProjectName
}

Here, How can I update the Project Name inside the respect <a></a> element ?

Upvotes: 2

Views: 66

Answers (2)

Dan Philip Bejoy
Dan Philip Bejoy

Reputation: 4391

If you could wrap the project name in a span, like

$("#divbindProjectlist").append('<a href="#" class="list-group-item projectList" ><span class="project-name">' + json.ProjectName + '</span><input type="hidden" class="hidprojectId" value="' + json.idPartnerProject + '" /></a>');

Assuming there is only one anchor tag, you could do

if (obj.idPartnerProject == pro.idPartnerProject) {
  //pro.ProjectName
  $("#divbindProjectlist a:has(input[value='"+json.idPartnerProject +"']) .project-name").html(pro.ProjectName);      
}

If you have multiple a tags youll have to keep unique ID's or change the selector to pick the correct one.

Upvotes: 1

Aditya
Aditya

Reputation: 89

Below function will change the link text

function changeAnchorText(t, i) {
    var n = $.map($("#divbindProjectlist a.projectList input[type='hidden']"), function(i) {
        return $(i).val() === t ? $(i).closest("a") : void 0
    })[0];
    if (n) {
        var d = $(n).find('input[type="hidden"].hidprojectId');
        $(n).text(i).append(d)
    }
}

First parameter will be hidden field value and second will be the text like changeAnchorText("0.19537117542457416","SomeName")

Upvotes: 0

Related Questions