Link to home
Start Free TrialLog in
Avatar of C C
C C

asked on

Click all buttons containing certain text on page with JQuery

I'm making a chrome extension, and basically id like to be able to pass it a certain word - like "Dog" and then it will click on every instance of "Dog" on the page. It should have a small time limit between each click. How would I do this? Whenever I've attempted this before I haven't been able to get it to work.
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Check this simple example.It takes every anchor element and via if condition it checks the attr href for existing state and the it opens a new window.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>

<title>HTML, CSS and JavaScript demo</title>
</head>
<body>
<!-- Start your code here -->
<ul>
  <li><a href="https://www.experts-exchange.com/">Dog</a></li>
  <li><a href="">Example</a></li>
  <li><a href="">A word</a></li>
 </ul>
 <p>This is a <a href="https://www.experts-exchange.com/questions/29075275/Click-all-buttons-containing-certain-text-on-page-with-JQuery.html">Dog</a> and this a <a href="">Cat</a></p>

<!-- End your code here -->
  
  <script>
function clickDog(arg){
 $('a:contains('+arg+')').each(function(){  
   if($(this).attr('href')!==null){
    window.open($(this).attr('href'));
   }
 });
}

clickDog('Dog');
    
  </script>
</body>
</html>

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.