Satch3000
Satch3000

Reputation: 49412

JQuery Click on a div which contains a specific text

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?

Upvotes: 2

Views: 7319

Answers (4)

Vaux42
Vaux42

Reputation: 760

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.

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

You can use contains-selector

Select all elements that contain the specified text.

$('.myclass li.active .short-text:contains("someText")').click();

Upvotes: 7

Sjoerd de Wit
Sjoerd de Wit

Reputation: 2413

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();

Upvotes: 2

Arko Elsenaar
Arko Elsenaar

Reputation: 1739

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

Upvotes: -3

Related Questions