-2

Im creating <td> with jquery function. And I want to change its value by using js function.

jquery is getting value and putting into table:

tr.append("<td id='stsChange'>" + resul[i].status + "</td>");

I want to change "<td id='stsChange'>" value if its 1 Pending, else if 2 Success.

Please help me to solve this problem, Thanks!

I have done till here, but its not working.

        function insertText () {

   var status = document.getElementById('stsChange').innerText;
   alert(status);
   if(status = "1"){
       document.getElementById('stsChange').innerHTML = "Pending";
   }
   else if(status = "2"){
       document.getElementById('stsChange').innerHTML = "Success";
   }

}
ABD
  • 113
  • 3
  • 12

2 Answers2

0

Simple solve with jQuery object

function insertText($tdObject) {
     var newTextVal = $tdObject.text() == '2' ? 'Success' : 'Pending';
     $tdObject.text(newTextVal); 
}
softbrewery
  • 501
  • 6
  • 18
0

Append is much easier with jquery (else you would need to use appendChild - ):

I have the following solution based on what you gave:
(Please run code snippet at bottom of answer to see the result:)

var result = [{status:1},{status:2},{status:1},{status:2}];
for(var i = 0, length = result.length; i < length; i++)
{
    $('#table_Status').append("<tr><td id='td_stsChange_" + i + "'>" + result[i].status + "</td></tr>");
  insertText(i);
}

function insertText(_i)
{
   var elem = document.getElementById('td_stsChange_' + _i);
   var status = elem.innerText;
   //alert(status);
   if(status === "1")
   {
       elem.innerHTML = "Pending";
   }
   else if(status === "2")
   {
       elem.innerHTML = "Success";
   }
}
#table_Status {
  border:1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Table:
<table id="table_Status">
  <th>Status</th>
</table>
clearlight
  • 12,255
  • 11
  • 57
  • 75
Andre Nel
  • 432
  • 2
  • 9