Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1348
  • Last Modified:

Find out if href contains text

Hi Experts :)

I want to achieve something like in the pseudo-code included.
I cannot find a good tutorial on this issue...

Can you tell me how to do that?

Best wishes,
The Terrible
if( $(this).text().contains("original") ){
$(this).text().replace("original", "false");
}
else{
$(this).text().replace("false", "original");
}

Open in new window

0
potworny
Asked:
potworny
  • 3
  • 2
1 Solution
 
cdaugustinCommented:
hi

as you yourself said you will use .text(). what you have to do is first get the value using simply .text() process it in a different variable and then put it back where you want it using .text(newVal) something like attached

(read the page at check http://docs.jquery.com/Attributes/text too)(posted this when very sleeeepy )(i dont like innerHTML in particular but since its just simple text... this should work just fine)
var tmpStr = "";
 
$(".classOfTheHrefs").each(
function callback(index, domElement) {
  this; // this == domElement
  
   tmpStr = this.innerHTML;
 
   //instead of false and orginal use your actual strings
   if (tmpStr.search() != -1)
   {
       tmpStr.replace(/original/, "false");
   }
   else
   {
       tmpStr.replace(/false/, "original");
   }
   
   this.innerHTML = tmpStr;
}
 
);

Open in new window

0
 
cdaugustinCommented:
and of course now that i see the post ive spotted a mistake, attached is updated code
var tmpStr = "";
 
$(".classOfTheHrefs").each(
function callback(index, domElement) {
  // this == domElement
   tmpStr = this.innerHTML;
 
   //instead of false and orginal use your actual strings
   if (tmpStr.search() != -1)
   {
       tmpStr = tmpStr.replace(/original/, "false");
   }
   else
   {
       tmpStr = tmpStr.replace(/false/, "original");
   }
   
   this.innerHTML = tmpStr;
}
 
);

Open in new window

0
 
potwornyAuthor Commented:
Shouldn't that look something like this?

   if (tmpStr.search("MyString") != -1)


   if (tmpStr.search() != -1)

Open in new window

0
 
cdaugustinCommented:
yes it should :) it was way to early in the morning to post answers, but i thought you will spot eventual errors.
0
 
potwornyAuthor Commented:
I would like to thank your for having your morning sacrifice to help my problem. THANK YOU :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now