1

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 ?

3
  • 1
    I'm just guessing here but wouldn't .text() just return a string and thus not be able to chain a .contains() method on it? Commented Apr 22, 2014 at 0:25
  • 1
    Strings in JavaScript don't have a .contains() method, though it's coming in ECMAScript 6 and currently available in Firefox. Chrome has it flagged as an experimental feature. Commented Apr 22, 2014 at 0:25
  • nice comment 'cookie monster', thanks. Thanks to everybody also, thats my 1st question and truely say, that I found answer before saw reply in diff StackO answer Commented Apr 22, 2014 at 0:56

4 Answers 4

1

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...
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

7 Comments

@RokoC.Buljan not sure I get what the IE8 comment is about?
indexOf doesn't exist in ie8
Ah, that's on an Array not a String -- which does work just fine.
To clarify... .indexOf() works in all versions of IE on Strings but in IE8 and below the Array object did not support an .indexOf() method.
@scunliffe +1 my bad must be the late hour... +1 this answer for being the first to answer.
|
1

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
}

1 Comment

This is something I was about to add as answer. +1
0

Also another way to do it with a single class

DEMO

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.