1

I have the following:

HTML

<div class="tab-pane" id="message">
      <textarea rows="4" cols="50" id="send_message" placeholder="Enter text ...">  </textarea>
      <a href="#message" class="btn btn-large btn-info"  data-toggle="tab">OK</a>
      <a href="#message" class="btn btn-large btn-info"  data-toggle="tab">Cancel</a>

JavaScript

$('#message').click(function(){
    if($("a", this).is(":contains(OK)")) {
        console.log("im in OK!!");
    } else if($("a", this).is(":contains(Cancel)"))  {
        console.log("im in cancel!!");
    }
});

This works fine when I hit the OK button and executes as expected, however when I hit cancel the code in OK is executed only. The cancel code never executes! What am I doing wrong?

2
  • Why should it execute if a:contains('OK') still exists in a #message? Commented Aug 12, 2013 at 18:17
  • 2
    It never enter the else if since $("a", this) contains both OK and Cancel Commented Aug 12, 2013 at 18:18

3 Answers 3

9

this is the element the event is bound to. It's always #message, therefore $("a", this) will always be the both buttons. .is will scan both buttons to see if any of them contain "OK". The first one does, so it always goes to the 1st block.

You should be binding the events to the buttons themselves, not the entire div.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm (obviously) new to JS , but I figured it would be easiest to understand to bind the div and then select the children input elements. Are you saying that $("a", this) always picks the first 'a' that it comes across in the context of this?
@user61629: I'd suggest that as a beginner, you should bind to each button individually. I was actually wrong. $("a", this) picks both. .is will scan everything in the set, so it always finds OK.
2

Detect clicks on a from #message, instead. this will become the clicked <a>.

$('#message').on("click", "a", function(){
    if($(this).is(":contains(OK)")) {
        console.log("im in OK!!");
    } 
    else if($(this).is(":contains(Cancel)"))  {
            console.log("im in cancel!!");
    }
});

JSFIDDLE

4 Comments

Fixing the code isn't useful without explaining what was wrong with the original code.
@meagar You are correct. I always prefer to be faster and then to edit my answer adding more content.
@meagar Anyway the antispam stopped me to be first... :-)
@Johnツ why would you choose to provide an incomplete answer faster over just providing the right answer?
1

Fist, I noticed you have a missing closing div (</div>)

Just curious, why not do the following:

$("#OK").click (function() {
    console.log("im in OK!!");
});

$("#Cancel").click (function() {
    console.log("im in Cancel!!");
});

<div class="tab-pane" id="message">
    <textarea rows="4" cols="50" id="send_message" placeholder="Enter text ...">  </textarea>
    <a href="#message" id="OK" class="btn btn-large btn-info"  data-toggle="tab">OK</a>
    <a href="#message" id="Cancel" class="btn btn-large btn-info"  data-toggle="tab">Cancel</a>
</div>

Cheers.

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.