0

I got inputs inside button. Is it possible to prevent triggering button click event when clicking on input? preventDefault() does not work.

<button class="btn_size">
<input type="text" name="size_y" style="width:60px;" />
<font style="font-weight:normal;">x</font>
<input type="text" name="size_y" style="width:60px;" />
Set size
</button>

http://jsfiddle.net/XeYTJ/

The same happens with stopPropagation(). It does not stop it:

http://jsfiddle.net/XeYTJ/2/

Few years ago I had similar problem. I wanted to be able to disable single accordion button. I wrote a function that was able to bind event at the top of any other events, and it just checked if clicked button had .disabled class. If yes, it simply did return false;. That worked. But only until jQuery 1.8 came up. Something changed in the access to event data, and it does not work anymore.

This question may seem to be easy, so I do aware that I may get alot of -1 votes. But please, check if You are able to resolve the problem, before voting.

2
  • event.stopPropagation()? Commented Aug 22, 2013 at 16:27
  • Still does not work... jsfiddle.net/XeYTJ/2 Commented Aug 22, 2013 at 16:39

1 Answer 1

1

You can make it work in two steps:

  1. Use the same name in the inputs as in the selectors. You have both inputs name="size_y", but you are using selectors $('input[name="y_size"]') and $('input[name="x_size"]'). See the difference?

  2. Add the click-event on the button before the inputs:

    $('.btn_size').button({icons: { primary: "ui-icon-arrow-4-diag" }}).click(function(){
        console.log('clicked');
    }); 
    
    $('input[name="size_x"]').click(function(e){
        console.log('x');
        e.stopPropagation();
    });
    $('input[name="size_y"]').click(function(e){
        console.log('y');
        e.stopPropagation();
    });
    

See fiddle.

And if that doesn't work, you could try changing <button> to <div> and use .button() to style it.

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

3 Comments

Oh indeed its size_x and x_size ... that is a grave mistake. I'm glad you noticed it. The question seems to be usless for most people then, but I won't delete it, because You really deserve to get points for noticing it.
@flashthunder well, not completely useless. It's still not that straightforward with the order in which to bind to the different click-events.
Yes you may be right, it is even possible that I tried before with good names but wrong order...

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.