1

I have this default jQuery selector (a div element) for matching a specific element:

$('.myselectorid')

However this "myselectorid" appears in different pages of the site. I am thinking if it is possible to add a page title selector to the above original selector so that I could execute my jQuery implementation on a specific page only (specified by the page title).

Also I need all of them combined as one jQuery selector.

I also know that to retrieve the page title, it would be like this:

$('html head title').text(); 

However I am not sure how to combined the above selector to my original selector as one new selector. For example, supposing I would like it to match the selector on a page with title "This is my page title", can I do it like this?

$('.myselectorid[html head title="This is my page title"]')

If not, how would I do it so that all of them appears as one combined selector (without using .add etc.) and it matches the page title as well?

I'm not an expert of jQuery selectors, I would appreciate if someone can share some insights on this approach. Thanks a lot!

Emerson Maningo
  • 2,189
  • 9
  • 32
  • 47

1 Answers1

1

I would add a class to the body tag and then check if I should run that code based on what page I am on.

if ($("body.className").length > 0) {
    // Place the logic pertaining to the page with the class
    //$('.myselectorid') code here
}

See this question for more ideas along this line.

Community
  • 1
  • 1
Blake Plumb
  • 6,779
  • 3
  • 33
  • 54