Reputation: 49412
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
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.
The key here is to use .text()
to access the element.
Upvotes: 0
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
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
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