jQuery change title to h1 title in activepage [duplicate]
-
25-06-2021 - |
Question
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;
});
OTHER TIPS
add id: <h1 id="mytitle" title="contact">contact</h1>
$("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();
// NEW:
$("#mytitle").text("Hello this is changed");
$("#mytitle").attr("title","we can also change here");
return false;
});
hope this is what you are looking for
make the title attribute match with the id of the current page, then
$("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();
$("h1[title="+activepage+"]").html("The new Title");
return false;
});
If you want to change the value of the title
of the page, you can try:
var current = $('h1').attr('title')
$('title').text(current)
// or document.title = current;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow