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>
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>
Try this script:
<script>
var name = "Myname"
$("#c0r0").text(name);
</script>
For this generated html page:
<table>
<tr>
<td id="c0r0">I have to print the name inside here</td>
<td id="c0r1">Dummy Text</td>
<td id="c0r2">Dummy Text</td>
</tr>
..............
</table>
$("#c0r0").text( name ); so that it's the contents of the variable inserted into the td, not a hard-coded string? Hope this helps!Check the Js fiddle
var name = "Myname";
$('#mytable tr:first td')
.each(
function()
{
//'this' represens the cell in the first row.
var tdtxt=$(this).html();
var n = tdtxt.concat(name);
alert(n);
}
);
Maybe make your own special tag....
var myDataModel = {
name: 'Sam Chalupka',
favoriteFruit: 'Lemon',
age: '45'
};
$('t').each(function() {
var key = $(this).attr('data');
var text = myDataModel[key];
console.log(this);
$(this).text(text);
});
In html...
<t data="name"></t>
<t data="favoriteFruit"></t>
<t data="age"></t>
Jsfiddle: