Search code examples
jqueryhtmlfunctiontitlepage-title

jQuery change title to h1 title in activepage


How can I change my active page title to h1 title or h1 content?

HTML

<section id="contact" class="page_content">
            <h2 title="Contact">Contact</h2>  
</section>

JavaScript

$("ul.pages li").click(function() {
     $("ul.pages li").removeClass("active");
     $(this).addClass("active"); 
     $(".page_content").hide();
     var activepage = $(this).find("a").attr("href")
     $(activepage).show();                          
     return false;
});

Solution

  • Try;

    $("ul.pages li").click(function() {
         $(this).addClass('active').siblings().removeClass('active'); 
         $(".page_content").hide();
         var activepage = $('a', this).attr("href");
         $(activepage).show();
         document.title = $('h1', activepage).first().text();   
         $('title').text( document.title );                        
         return false;
    });