vlada vlada - 17 days ago 9
CSS Question

Change Title's content on tab click

I have a little problem. I want to change my page title when I click on 1 of 5 tabs that I have on my page. The title of that page should change based on what tab was clicked. If would go something like this:

<div id="tab1"></div>
<div id="tab2"></div>
<div id="tab3"></div>
<div id="tab4"></div>
<div id="tab5"></div>


When clicked on tab1, title should change to "tab1, when clicked on tab2, title should change to tab2 and so on...

I tried with this jquery, but it didn't work. :)

<script type="text/javascript">
$(document).ready(function (){
('#tab2').click(function ()}{
(document).title ('tab2');
})
});




Can you please help me with that?

Thank you....

Answer

Try this

<div class="change-title" id="tab1">1</div>
<div class="change-title" id="tab2">2</div>
<div class="change-title" id="tab3">3</div>
<div class="change-title" id="tab4">4</div>
<div class="change-title" id="tab5">5</div>

<script>
$(document).ready(function() {
    $('.change-title').on('click',function(){
         document.title = $(this).attr('id');
    });
});
</script>