Jamie Taylor
Jamie Taylor

Reputation: 3530

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

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_lblNoResults" 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

Upvotes: 0

Views: 114

Answers (2)

Nick Craver
Nick Craver

Reputation: 630389

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

Upvotes: 0

Stephan Muller
Stephan Muller

Reputation: 27600

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)

Upvotes: 1

Related Questions