-1

I am working with jira and there are two buttons with the same id and class. Both are submit button, one at top and one below and when i use the below jquery only the first one is captured while clicking. Intention is to make comments testfield mandatory.

Only the top one is working..

<script>
AJS.toInit(function () {
   AJS.$("#next").click(function(e){
    var comment=AJS.$("#jira #page #content .aui-page-panel .aui-page-panel-inner .aui-page-panel-content #bulkedit .form-body .aui .comment-input #comment").val();
    if(comment==""){
    AJS.$(".bulk-affects").append("<br/><div id='resErrror' style='color:red;margin-left:30px'>Comment is mandatory</div>");
       return false;
       }

   });

   });
</script> 

Please help me out with to capture both the next button to work for this script

Sijo Kurien
  • 95
  • 2
  • 10
  • 1
    Does this answer your question? [JavaScript and getElementById for multiple elements with the same ID](https://stackoverflow.com/questions/3607291/javascript-and-getelementbyid-for-multiple-elements-with-the-same-id). Also [Dealing with Two Elements with same ID in JavaScript](https://stackoverflow.com/questions/42941158/dealing-with-two-elements-with-same-id-in-javascript/42941208) – Umair Khan Aug 24 '20 at 08:51

3 Answers3

2

ID's should be unique, so you should only use a particular ID once on a page. Actually when you try to call the element using id code only calls the first element. Classes may be used repeatedly

1

querySelectorAll will find all ID's

const allButtons = document.querySelectorAll('#next');
for (let button of allButtons) {
  $(button).click((e) => {
    // Do your magic here
  })
}
Anton
  • 2,669
  • 1
  • 7
  • 15
0

Remember: A webpage element has certain inalienable rights; having a unique ID is one of them. Prevent html element identity theft by ensuring that every element on your webpage that needs an ID has a unique one.

But Classes can be repeated.

Vinayak
  • 80
  • 8