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

potwornyAsked:
Who is Participating?
 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.