Changing the elements on a page by grabbing the title

Hello all, just going through the jquery api and trying things out.
this is not quite working.
I check for the title of the page, alert the title on reload.
but when i went to change some elements if the title matches the pages it is on. It does nothing.

jQuery(document).ready(function () {
    var $title = jQuery(this).attr('title');
	alert(title);
	if (document.prop('title','Resources |  digital') == $title){
	$( "a" ).wrap( '<div class="testClass"></div>' );
		};
	});

&

.testClass{
border:3px solid #ffff00;
display:block;
padding:10px;
}

In your above page you are attempting to actually change the page title, and only fail at doing that due to a misunderstanding of how jQuery is used.

If you want to use jQuery methods to check the page title, you will have to turn document in to a jQuery resource.

if ($(document).prop('title') === 'Resources |  digital') {
    ...
}

And, it’s even easier to achieve just by using standard JavaScript

if (document.title === 'Resources |  digital') {
    ...
}

I can’t help but feel though that checking the title of the page is a brittle technique, and that there has to be a more robust manner in which to achieve the same result.

thx Paul. will try it.
Why “brittle”?
D

yeah tried both jquery & js version still no dice. got the alert back but don’t see why i can’t enact a change.

  var title=$('title').text()
	alert(title);
	if (document.title === 'Resources |  digital') {
   $( "p" ).wrap( '<div class="testClass"></div>' );
	};

never mind. error was due to a typo. works once that is fixed.
been advised it is a bad idea to use the title as people could change it.
D

Yes, and it also means that the script is on all other pages that don’t need the change too.

What sort of system is being used to build the pages? Most of them have ways for custom content (such as a separate script) to be included on certain pages.

lately i just have been using wp at work we have wp & cq5/AEM
but this exercise was just for me so i can get a better grasp of jquery in general.
went to the jquery api doc and thought i’d start working my way down.
Was also curious as to grab a specific page if needed.
so if i was in wp and wanted to only affect one page w/the code i could grab that page w/jquery

D

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.