1

I'm trying to run this code, but it breaks my entire page. The page is marked up like this:

<script type="text/javascript">
$(document).ready(function() {
  $("#right_column h1").each( function() {  
   var link = $(this).html()+"<br />";
   document.write(link); 
});
});
</script>
<div id="right_column">
<h1>Company Profile</h1>
blablablablabla<br />

<h1>Nulla turpis nunc, dapibus ultricies.</h1>
blablablabla<br />
<h1>Pellentesque habitant morbi tristique proin laoreet.</h1>
blablablabla<br />

When i try to run the code, it only shows me the content of the 3 h1's, the rest of the page (the h1 themselves) aren't loaded anymore. When i remove the $(document).ready() function, and put the script block after everything, it works just fine.

j08691
  • 190,436
  • 28
  • 232
  • 252

2 Answers2

4

Don't use document.write after the document is read.

If you want to append to the page, do this :

  $(document).ready(function() {
     $("#right_column h1").each( function() {  
       var link = $(this).html()+"<br />";
       $(document.body).append($(link));   
     });
   });
Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
  • Changing it to this worked-> $(document).ready(function() { $("#right_column h1").each( function() { var link = $(this).html()+"
    "; $("#location_where_i_wanted_them_to_show").append(link); }); }); The $(link) should be just link
    – Pim van den Broek Apr 17 '13 at 19:04
0

I will use something like this:

<script type="text/javascript">
$(document).ready(function() {
  $("#right_column h1").each( function() {  
   var link = $(this).html()+"<br />";
   $("#container-element").html('<a href="' + link + '">TEXT-YOU-WANT</a>');
});
});
</script>
Alex Abreu Mulet
  • 118
  • 1
  • 1
  • 8