Psl
Psl

Reputation: 3920

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>

Upvotes: 1

Views: 10545

Answers (6)

Ladislav Lig&#225;rt
Ladislav Lig&#225;rt

Reputation: 11

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:

Upvotes: 0

Serj Sagan
Serj Sagan

Reputation: 30208

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>

Upvotes: 4

Arun Bertil
Arun Bertil

Reputation: 4638

Check the Js fiddle

http://jsfiddle.net/5EXtz/

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);
                }  
            );

Upvotes: 1

George
George

Reputation: 628

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>

Upvotes: 3

Vikas Gautam
Vikas Gautam

Reputation: 978

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<script>

$(document).ready(function(){
var name = "Myname"

$("#result").html(name);

})



</script>


<div id="result">i have to print the name inside here</div>

Upvotes: 4

Arun P Johny
Arun P Johny

Reputation: 388316

Add a identifier to the target td like a class or an id

<td class="name">i have to print the name inside here</td>

then

jQuery(function(){
    $('td.name').text(name)
})

Note: since it is tagged using jquery, I assume jQuery library is already added

Upvotes: 3

Related Questions