0

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.

4
  • Can you explain what you're trying to do a little more. How does the snippet of code you've posted relate to the title? What value are you trying to get from title, and what do you want to do with it? Commented May 8, 2015 at 6:52
  • Hi Rory. I want to run the script only if the <title> tags contains a specific string. @RoryMcCrossan Commented May 8, 2015 at 7:11
  • Ok, but what is that string? And where does it come from Commented May 8, 2015 at 7:12
  • The string belongs to the specific customers information. Its the name of the company of that customer. When the customer logs in this adds to the <title> tags. I want to use my script when this strings is meet in the title tags. Commented May 8, 2015 at 7:36

3 Answers 3

2

here is the code

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

Actually.. It changes the strings even if title doesnt contains the specific string. So close! @AdarshGowdaKR
0

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.

1 Comment

Hi Jai. Not sure how this would work? I would want to run the script that changes the divs string only when the <title> tag contains a specific string? @Jai
0

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")
});

2 Comments

I am succesfully changing the text in the divs with the script I posted - Im looking to run the script only when the <title> tags have a certain string in them.
var title = $("title:contains('demo')"); // you may want to retrieve the full text of the title var value = title.text(); if(title){ //your code here }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.