-1
function btn1(){
alert('hello')
}
function btn2(){
alert('anchor') 
}

I want to call the function btn1 and function btn2 separate on clicking the div. As it is shown below.

<div onclick="btn2()" style="position:relative">
<div style="width:50%; float:left; height:50px; background:#ddd">
</div>
<input type="button" value="hello" onclick="btn1()"/>
</div>
Jimi Gautam
  • 17
  • 1
  • 2
  • Hi guys i want to call the function separately, suppose I click on entire div it should call the function btn2 and if I click on the input button it should call the function btn1. – Jimi Gautam Mar 13 '14 at 17:52
  • 3
    it's exactly how you have it there. are you experiencing difficulty with it? – ddavison Mar 13 '14 at 17:52
  • 1
    Also make sure the JavaScript code is in a script tag. :) – Peter Mar 13 '14 at 17:54
  • separate or one of the functions together with the other as in function btn2(){ alert('anchor') btn1() } – sqlab Mar 13 '14 at 17:55
  • Just a friendly advice, is not necessary but it's good practice, use ";" at the end of the line of the alerts – PlaceUserNameHere Mar 13 '14 at 17:56
  • It is in script tag. If i clicking on the input button then btn2 function is also happening. I want to call only btn1 function on clicking the input button. – Jimi Gautam Mar 13 '14 at 17:57
  • take a look here: http://stackoverflow.com/questions/387736/how-to-stop-event-propagation-with-inline-onclick-attribute – Sebastian Otto Mar 13 '14 at 17:59

2 Answers2

3

If I am getting this right you are probably experiencing bubbling. You might want to read about bubbling and event propagation as it will probably be clearer than what I can try to explain. But to solve your issue you want to take a look at jquery's

event.stopPropagation() 

http://www.w3schools.com/jquery/event_stoppropagation.asp

martskins
  • 2,920
  • 4
  • 26
  • 52
0

The Click Event of the button bubbles his way up to the div. Take a look here how to prevent this:

How to stop event propagation with inline onclick attribute?

I recommend you use addEventListener in favor of inline event handler.

Community
  • 1
  • 1
Sebastian Otto
  • 15,139
  • 4
  • 18
  • 21