Reputation: 477
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
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
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
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:
var title
as posted.var title
in the code with concatenation.Upvotes: 0