jquery to set html title tag from span element

Posted on 2014-02-12
Last Modified: 2014-02-12
I am trying to set an html page <title> tag with text on the page.  The page is a dynamic page, so I want the page title to change depending on the dynamic page opened - which will have different text on every page.

I'm close to getting this to work - but I need help with my jquery syntax.  I don't fully understand how to set this with jquery, so I'd appreciate a little information about how/why your recommendation will work vs. what I did that is almost working.

This is my script:

            $(document).ready(function () {
                var strTitle = $('.resourcetitle');
                $(document).attr('title', strTitle);

Open in new window

This is what renders in the title tag and browser tab (in correctly displaying [object Object])
title tag error
This is how the text on the page is wrapped on the page:

    <td class="ms-vb" width="25%"></td>
    <td class="ms-vb" width="75%">
        <span class="resourcetitle"></span>


Thank you in advance for a reply.
Question by:Sue White
  • 2
  • 2
LVL 17

Accepted Solution

OmniUnlimited earned 500 total points
ID: 39854244
Correct me if I'm wrong, but it appears like you are trying to place the text from your span tag into the title?  If that is the case, you need to access the .text() function:

            $(document).ready(function () {
                var strTitle = $('.resourcetitle').text();
                $(document).attr('title', strTitle);

Open in new window


Author Comment

by:Sue White
ID: 39854251
Ah.... so that's what I was missing.  Yes, this works!  Thank you very much.

Author Closing Comment

by:Sue White
ID: 39854254
thanks for the accurate and speedy reply
LVL 17

Expert Comment

ID: 39854260
You are welcome.  Glad to help, and thanks for the points!

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery autocomplete 10 68
JS Plugin 4 29
Angular- another way of coding hide and display 3 39
Angular - "nest" service calls? 18 23
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now