最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery if div contains text, toggle button - Stack Overflow

programmeradmin2浏览0评论

I'm trying to get a button to be toggled if a div contains a string of text. Something like:

$(document).ready(function(){
$(function() {
if ('$div#trackingnumber:contains('Hello')');') {
    $("dinput#submitam").toggle()});
}
});

Does anybody know how to do this?

I'm trying to get a button to be toggled if a div contains a string of text. Something like:

$(document).ready(function(){
$(function() {
if ('$div#trackingnumber:contains('Hello')');') {
    $("dinput#submitam").toggle()});
}
});

Does anybody know how to do this?

Share Improve this question asked Feb 2, 2012 at 1:56 henryaaronhenryaaron 6,21221 gold badges63 silver badges82 bronze badges 1
  • a call of $() will always return an object, no matter if the selector matches anything, so a boolean parision is useless there(an object is never false). You need to check the length of the result (like suggested by dknaack) to determine if the result was empty. – Dr.Molle Commented Feb 2, 2012 at 2:04
Add a ment  | 

3 Answers 3

Reset to default 8

You are on the right way. I only see some syntax errors. Try this

$(document).ready(function(){
     if ($("#trackingnumber:contains('Hello')").length != 0) 
     {
         $("dinput#submitam").toggle();
     }
});

Checkout this jsFiddle too.

Just do like this

$(document).ready(function(){
  if ($("div#trackingnumber:contains('Hello')").length > 0)
  $("dinput#submitam").toggle();
});
$(document).ready(function() {
    if ( $("#trackingnumber:contains('Hello')").length > 0 ) {
        $("#submitam").toggle();
    }
});
发布评论

评论列表(0)

  1. 暂无评论