Psl Psl - 6 months ago 20
HTML Question

print Jquery string values inside html

In the following code is there any possible way to print the string name inside

td





<script>

var name = "Myname"

</script>


<td>i have to print the name inside here</td>
</tr>
</table>

Answer

Here's a way if you can't change any of the html markup:

<script>
    var name = "Myname"

    $(document).ready(function(){
         // Replace all with `name`
         $('td:contains("i have to print the name inside here")').text(name);

         // Add `name` to end
         $('td:contains("i have to print the name inside here")').append(name);

         // Add `name` to beginning
         $('td:contains("i have to print the name inside here")').prepend(name);

         // etc.
    });

</script>