开发者

find out if a id has certain text and then change it

开发者 https://www.devze.com 2023-01-15 11:42 出处:网络
I need to something like this if($(\'#ctl00_lblNoResults\').text(\'Please select\')){ $(\'.otherprop\').text(\'Thanks\')

I need to something like this

if($('#ctl00_lblNoResults').text('Please select')){
$('.otherprop').text('Thanks')
}
else {
$('.otherprop').text('No Thanks')
}

So i have a layer like so

<div id="ctl00_lblNoResults">Don't select</div>

And another bit of text

<div class="otherprop">No Thanks</div>

I need to change the text of "otherprop" if "ctl00_lb开发者_高级运维lNoResults" equals this

<div id="ctl00_lblNoResults">Please select</div>

to

<div class="otherprop">Thanks</div>

So desired results are

<div id="ctl00_lblNoResults">Don't select</div>
<div class="otherprop">No Thanks</div>

or

<div id="ctl00_lblNoResults">Please select</div>
<div class="otherprop">Thanks</div>

Hope this makes sense

Thanks

Jamie


if($('#ctl00_lblNoResults').text() == 'Please select')
$('.otherprop').text('Thanks')
}
else {
$('.otherprop').text('No Thanks')
}

should do it

the if-rule you stated sets the text already, after which it will return true if it was successful (probably false if the id doesn't exist)


First, what you have should work. All you could do is maybe use a conditional statement when setting .text() to keep it pretty clean (to me, do what's best for your maintaining it of course), like this:

$('.otherprop').text(
  $('#ctl00_lblNoResults').text() == 'Please select' ? 'Thanks' : 'No Thanks';
);
0

精彩评论

暂无评论...
验证码 换一张
取 消