Xeptor
Xeptor

Reputation: 477

use string in <title> to change string in div

I am using a jquery script to change strings to something else based on the divs initial string as well as adding an extra class to the element. Works great.

However, I just got in a situation that I need to be able to target a few divs based in a string in the sites title tags when a special users logg in thus the title change.

Is this possible? Both Vanilla JS and jQuery is good for me.

Script I am using:

 $( "span.conditionHilite:contains('Standard')" )
  .text('Approved Selection').addClass( "approvedSelectionHilite" );

I would like to run that script ONLY if the title contains a certain string.

Upvotes: 0

Views: 115

Answers (3)

Adarsh Gowda K R
Adarsh Gowda K R

Reputation: 951

here is the code

if($('title:contains("Your specific string")')){
// do your task 
$( "span.conditionHilite:contains('Standard')" )
  .text('Approved Selection').addClass( "approvedSelectionHilite" );

}

Upvotes: 2

Antonio
Antonio

Reputation: 654

You should use the the syntax $( "[attribute='value']" ) Here the doc https://api.jquery.com/attribute-equals-selector/

For example you could change the text of all the divs having title=Standard

$("div[title='Standard']).each(function(index,value){    
    $(this).text("change-me")
});

Upvotes: 0

Jai
Jai

Reputation: 74738

I just got in a situation that I need to be able to target a few divs based in a string in the sites title tags.

you can cache it before like this:

var title = document.title.trim(); // <---gives you the title of the document.
$( "div:contains("+title+")").css('border', 'solid red 1px');

When you use the code snippet above you can see what is it doing:

  1. caching the document title in a var title as posted.
  2. When you cache it you can use this var title in the code with concatenation.
  3. Just for a demo purpose i have attached the css method of jquery.

Upvotes: 0

Related Questions