2 Replies - 925 Views - Last Post: 31 January 2012 - 04:00 PM

#1 SittingonDucks   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

This seems to be... wrong.

Posted 28 January 2012 - 08:52 PM

I have two problems:
  • The list items are being written OUTSIDE of the table.
  • The 'radius+5' seems to make the loop run about 500 more times.


Here is my code portion:

			$("#rofl").html('<table id="ready" class="ready"><th>Radius</th><th>Circumference</th><tr><td class="clicked">'); 
			for (var intNum=val; intNum<=5+radius; intNum++) 
			{  
				console.log(intNum); 
				if (intNum != val+5) 
				{    
				 $("#rofl").html($("#rofl").html() + " " + intNum + " </td><td class='clicked' ison='n'>"); 
				 intRad = intNum * 2 * Math.PI
				 $("#rofl").html($("#rofl").html() + " " + intRad + " </td></tr><tr><td class='clicked' ison='n'>");
				}
				else 
				{ 
				 $("#rofl").html($("#rofl").html() + " " + intNum + " </td><td class='clicked' ison='n'>"); 
				 intRad = intNum * 2 * Math.PI  
				 $("#rofl").html($("#rofl").html() + " " + intRad + " </td></tr>");
				} 
			}
			$("#rofl").html($("#rofl").html() +'</table>'); 


Is This A Good Question/Topic? 0
  • +

Replies To: This seems to be... wrong.

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: This seems to be... wrong.

Posted 29 January 2012 - 01:17 AM

what list items? I’m not sure if jQuery uses anythings else than .innerHTML in its .html() method, but tables often cannot be build using .innerHTML.
Was This Post Helpful? 0
  • +
  • -

#3 AdaHacker   User is offline

  • Resident Curmudgeon

Reputation: 463
  • View blog
  • Posts: 820
  • Joined: 17-June 08

Re: This seems to be... wrong.

Posted 31 January 2012 - 04:00 PM

Your conceptual model is wrong. The jQuery.html() method and innerHTML property just don't work that way.

You're treating the node innerHTML as a sort of string builder. But it's not. When you set the HTML for an element, it doesn't just dump random text into the element. Rather, it generates DOM nodes which the browser then renders. So if you pass $('#rofl').html() a bunch of half-finished markup, the browser is going to try to "fix" your markup in such a way that it will actually render, just as it does when the source document contains broken markup. So when you try to get the node HTML again later, $('#rofl').html() isn't going to return the same garbage markup that you passed in on the last call.

So if you want to build up your table HTML this way, you need to store it in a string variable and then set the $('#rofl').html() when you're all done. But doing things like $("#rofl").html($("#rofl").html() + " " ... is just never going to work.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1