0

I have my HTML structure in the following format

<div>
<form class="form-inline" style="padding-top:10px" action="javascript:function()">   <!-- a search bar-->
  <input id="id" type="text" " placeholder="Something">
   </form>

<button onClick="somefunction()"> b</button>
</div>

the problem is I want the search bar to work when I press enter in it but unfortunately it is submitting the button element , I am slightly confused as the button is not a form element and I have specifically defined onClick. I am new to javascript so I am sure it is something small but I have not been able to solve it.

ie when I press enter , the "somefunction()" gets active as opposed to "function()"

alex
  • 235
  • 3
  • 11

2 Answers2

0

You may put your button inside the form and have the function called in onClick return false, as discussed here.

EDIT

According to your edit and your comments on what you want, you may do the following:

<html>
<head>
    <script>
        function formfunction(){
            alert("form function is called.");
        }
        function somefunction(){
            alert("the button is clicked.");
        }
    </script>
</head>
<body>
<div>
<form name="myform" action="#" onsubmit="formfunction(); return false;">
    <input id="id" type="text" placeholder="Something">
</form>
<button type="button" onClick="somefunction()"> b</button>
</form>
</div>
</body>
</html>
Community
  • 1
  • 1
Kamyar Infinity
  • 2,711
  • 1
  • 21
  • 32
0

Try this

HTML

<div>
    <form class="form-inline" style="padding-top:10px" action="someFile.php" onsubmit="return someFunc();"> 
        <input id="id" type="text" placeholder="Something">
        <input type="submit" name="btn_submit" value="Submit" /> 
    </form>
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

JS

function someFunc(){
    // code goes here
    return false;
}

Just an example.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Thank you , but the button condition just popped up again ! Did not work – alex Aug 14 '12 at 22:08
  • Well, I think you want to execute `sumeFunc` on button press and this answer is all about that. Did you check the example ? – The Alpha Aug 14 '12 at 22:12
  • I did , but maybe I am still a little confused , on the button click I want another function to be executed and on return I want the function linked with the form element to be executed – alex Aug 14 '12 at 22:24