javascript - Jquery .click() not working multiple button -


i want show alertbox on click button more of button id attr , work click on first button not working click on second button.

jquery;

jquery("#submit").click(function(e){      alert("message"); }); 

html; (repeat html per message)

 <div class="reply">    <form id="vivam" method="post" onsubmit="return false;">        <textarea name="reply" placeholder="write reply here"></textarea>       <p class="stdformbutton">           <button id="submit" class="btn btn-primary">reply</button>        </p>    </form>  </div> 

jsfiddle link

what solution problem? thank in advance answers.

approach 1: ids should unique, use class instead of id.

html:

<div class="reply"> <form id="vivam" method="post" onsubmit="return false;">    <textarea name="reply" placeholder="write reply here"></textarea>   <p class="stdformbutton">       <button class="btn btn-primary submit">reply</button>    </p>  </form> </div> 

js:

jquery(".submit").click(function(e){  alert("message"); }); 

working demo approach 1

approach 2: can use attribute value selector target element same id. breaks rule ids should unique.and not recommend using this:

jquery("[id=submit]").on('click',function(e){  e.preventdefault();   alert("test"); }); 

working demo approach 2


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -