2

I am trying to click on a div while contains a specific text.

This is where I am:

$( document ).ready(function() {

$('.myclass li.active .short-text'["value=thetext"]).click();


});

The code above is not doing it ... How can I get this to work?

Satch3000
  • 47,356
  • 86
  • 216
  • 346

4 Answers4

6

You can use contains-selector

Select all elements that contain the specified text.

$('.myclass li.active .short-text:contains("someText")').click();
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
2

this is the jquery method of checking if something contains a text. i don't know if you can use it as a click function though

   $('.myclass li.active .short-text:contains("thetext")').click();
Sjoerd de Wit
  • 2,353
  • 5
  • 26
  • 45
0

This should do what you like. On clicking the div, it checks the divs text contents, and in this case if it equates to "My Text", you'll get an alert.

DEMO

The key here is to use .text() to access the element.

Community
  • 1
  • 1
Vaux42
  • 750
  • 5
  • 7
-3

Something like this should do the trick. Not sure if there is a better solution.

$(document).ready(function() {
    $("div").click(function() {
        if($(this).text() == "The text") {
            //Code here
        });
    });
});
Arko Elsenaar
  • 1,689
  • 3
  • 18
  • 33