0

I have a <button> element which adds an element to my page when clicked.

What I'd like to do is to have an <input> inside this button, in which I could input a number, and then on button click, it would add x times the element instead of clicking x times on the <button>

See demo here : http://jsfiddle.net/MWxgb/5

The problem is that I can't click inside the <input> element inside the button, it clicks the button instead.

Is there a way to avoid this behavior ?

Erik
  • 88,732
  • 13
  • 198
  • 189
Sylvain
  • 3,823
  • 1
  • 27
  • 32
  • That's certainly interesting. FWIW, the input element doesn't render at all in IE9. – Sapph Mar 29 '11 at 07:24
  • That's weird, it renders correctly on IE7 (all squared, but it renders) – Sylvain Mar 29 '11 at 07:31
  • Thrd: Switching to IE7 or IE8 mode causes the input to show up. I suspect nesting an input field inside a button might be non-standard, but that'd require looking at the spec. :) – Sapph Mar 29 '11 at 07:32
  • Thrd: Yup, looks like it's illegal HTML to have interactive content descended from a `button`. Consider using a `div` or some other method to accomplish your goal. - http://www.w3.org/TR/html5/the-button-element.html#the-button-element – Sapph Mar 29 '11 at 07:35

3 Answers3

2

Just prevent the clicks on the <input> from bubbling:

$("#count")
    .click(function(e) {
        e.stopPropagation();
    });

http://jsfiddle.net/qT7gC/

Alex Gyoshev
  • 11,929
  • 4
  • 44
  • 74
1

http://jsfiddle.net/3vtTv/2/

I changed the button to a div, but still use $("#btn").button() to style it.

Then I call stopPropagation on the click event for the #count click handler.

This seems to work (in IE9, Chrome 10, FF4), but unfortunately the button still flashes when you click the textbox. Not sure how to work around that.

Sapph
  • 6,118
  • 1
  • 29
  • 32
0

Odd idea, I doubt that would work. I would recommend just using a text input, and setting up some javascript to read a click/enter key pressed. Refer to this question

Here is an example

Community
  • 1
  • 1
Jess
  • 8,628
  • 6
  • 49
  • 67