Reputation: 2478
Is it possible to add a div-element to a table cell/td by using jquery? In my HTML code I have have div called s (just for testing):
<div id="s">12</div>
In my $(document).ready(function(){
function I I have added the following $("#sale_graph1").html('<div id="s"></div>');
where sale_graph1
is the id of the <td>/cell
i want to add the <div>
-element to. The cell gets populated with text if I write $("#sale_graph1").text
so I know it is the right cell I'm operating on. But when trying to add the div content nothing shows up in the cell.
Thanks for any help!
Upvotes: 0
Views: 934
Reputation: 36
It seems the code is right? But I think maybe some errors at some place.
You can check it like as follow: 1. check if you got the right dom
console.log($('#sale_graph1').length);
insure that you can get '1' in the debugger tool's console
if you pass first step, then you can do
$("#sale_graph1").html('<div id="s">some text</div>');
to make sure you add some text in div#s
then if it dosen't work, you can say the four words;
Upvotes: 0
Reputation: 4628
This working just fine. See here: http://jsfiddle.net/va5gr/
$('#sale_graph1').html("<div id='s'>12</div>");
(I've added the css to make it more visible)
If however you want to use the contents of your existing div#s
then you can do this:
$('#sale_graph1').html($('div#s').html());
Upvotes: 2
Reputation: 4578
Try this code:
$("#sale_graph1").append($('#s'));
and ensure that you have only one element called sale_graph1
Upvotes: 2
Reputation: 82241
Is this what you want:
$("#sale_graph1").html($('#s').html());
Upvotes: 1
Reputation: 3775
After this line:
$("#sale_graph1").html('');
You can add stuff inside your div doing this:
$("#sale_graph1 #s").text('12'); //to add text
or
$("#sale_graph1 #s").html('12'); //to add text with html content
Upvotes: 1