2

I have a html form with a select, button and an input element.

<form action="">
    <button>innocent button</button>
    <select multiple name="multiple">
    <option selected value="a">A</option>
    <option value="b">B</option>
</select>
<input style="width:300px" type="text" value="press here enter and look at the multiple select" name="" />
</form>

and some jquery javascript

$(document).ready(function(){
            console.log('hi');


            var $button = $('button');

            $button.on('click',function(e){

                $('select option').first().attr('selected',false);
                e.preventDefault();

            });

Demo: try it here: http://jsfiddle.net/3Rjdh/

On Chrome everything is okay. But on Firefox: If you press ENTER in the input field, the select element loses it's selected.

What is wrong with Firefox?

2 Answers2

0

When you press enter on the input, you are effectively firing the click event of the button, trying putting a conole.log in there and you'd see it fire

You can stop the submission by doing something this

  function stopSubmit(e){
      e = e || event;
      return (e.keyCode || event.which || event.charCode || 0) !== 13;
   }

Then in your form add the event for keypress

    <form onkeypress="return stopSubmit(event)">

See the updated fiddle

Community
  • 1
  • 1
Hazem Salama
  • 14,891
  • 5
  • 27
  • 31
0

I think, I fixed it by adding the attribute type with value button

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