Reputation: 365
Google doesn`t execute my JS code. In Firefox everything works fine
$('a[href="#pricends"]').click(function(){
$("div.text_1").text(($("div.text_1").text() == 'smth1') ? 'smth2' : 'smth1')
$("div.text_2").text(($("div.text_2").text() == 'smth2') ? 'smth1' : 'smth2')
if ( $('div.text_1').text().contains("smth1") ) {
//smth here
}
Chrome console output points to "if" line above.
Uncaught TypeError: undefined is not a function (anonymous function) f.event.dispatch jquery-1.7.2.min.js:3 h.handle.i
How to fix this? Nothing goes at mind now, name function and compare it with bool before if ?
Upvotes: 1
Views: 5135
Reputation: 206102
Also another way to do it with a single class
<a href="#pricends">CLICK</a>
<div class="smth">smth1</div>
<div class="smth">smth2</div>
jQ:
var $smth = $(".smth"); // Cache your elements
$('a[href="#pricends"]').click(function( e ){
e.preventDefault(); // Prevent Browser default anchor behavior
$smth.text(function(i, txt){
return txt=='smth1'?'smth2':'smth1';
});
});
Somewhat extended:
var $smth = $(".smth"); // Cache your elements
$('a[href="#pricends"]').click(function( e ){
e.preventDefault();
$smth.text(function(i, txt){
if(txt==="smth1"){
// do something with $(this)
// or do something else
}
// and afterwards thange text
return txt=='smth1'?'smth2':'smth1';
});
});
Upvotes: 0
Reputation: 9128
Why don't you just use the same check you used when choosing which text to append:
if ( $('div.text_1').text() === "smth1" ) {
// do it
}
Upvotes: 1
Reputation: 63588
You can't call .contains() on the text value returned from .text().
If you test the value after extracting it with jQuery you should be ok.
var divText = $('div.text_1').text();
if(divText.indexOf("smth1") != -1){
//do your stuff...
}
Upvotes: 1
Reputation: 19358
Use something like this:
if ( $('div.text_1').text().indexOf("smth1") !== -1 ) {
//smth here
}
The indexOf
will return something other than -1 if "smth1" exists somewhere in your text.
Upvotes: 1