How to determine the press and run the function but at the same time use the same function for many repetitive forms. Now you understand what I mean.

There is such a form. On the page there may be several such depending on the number of rows in the database (not the essence).

<div> <textarea id="comment_menu"></textarea> <p id="submit-kom" iid="x">Отправить</p> </div> 

But the script defines the click and runs the function

 $('#submit-kom').click(function(){ var comment = $("#comment_menu").val(); var iid = $("#submit_kom").attr("iid"); if (comment != ""){ comment_menu = '1'; $("#comment_menu").css("borderColor","#DBDBDB"); }else{ comment_menu = '0'; $("#comment_menu").css("borderColor","#FDB6B6"); } if ( comment == '1'){ $("#submit-kom").hide(); $.ajax({ type: "POST", url: "../xxx/xxx.php", data: "id="+iid+"&comment="+comments, dataType: "html", cache: false, success: function(data) { if (data == 'yes'){ $("#submit-kom").show(); } } }); } }); 

The problem is that there is more than one button on the page to оправить with id="submit-kom" and therefore the function does not work by clicking on one of the buttons. How to make everything work? maybe via onclick or something similar, it is necessary that js determines that the отправить button is pressed but with a specific индексом or id

  • function (e) {var currentClick = e.target.attr (id); } read learn.javascript.ru/obtaining-event-object . Plus id is a unique thing and is only available on the page once, just put attributes on buttons and catch the attribute e.target and everything will be beautiful - Lieutenant Jim Dangle
  • @Lieutenant Jim Dangle, thank you, I’ll read everything now and try to do something - Ruslan Liashenka

1 answer 1

Do not use duplicate id , but use duplicate classes.

(Where does the variable comments come from?)

 <form> <div> <textarea class=".comment_menu"></textarea> <p class=".submit-kom" iid="x">Отправить</p> </div> </form> $('.submit-kom').click(function() { var commentMenu = $(this).closest("form).find(".comment_menu"); var comment = commentMenu.val(); var iid = $(this).attr("iid"); var comment_menu; if (comment != "") { comment_menu = '1'; commentMenu.css("borderColor","#DBDBDB"); } else { comment_menu = '0'; commentMenu.css("borderColor","#FDB6B6"); } if ( comment_menu == '1') { $(this).hide(); $.ajax({ type: "POST", url: "../xxx/xxx.php", data: { id: iid, comment: comment }, dataType: "text", cache: false, success: (function(data) { if (data == 'yes'){ $(this).show(); } }).bind(this) }); } }); 
  • Thanks for the answer, now I'll try everything - Ruslan Liashenka
  • comments was originally taken from <textarea id="comment_menu" ></textarea> - Ruslan Liashenka
  • something especially nothing happens after pressing - Ruslan Liashenka
  • Hello, I figured out the code and it all worked, thank you very much. Do not tell me how to add $('.message-baron').attr("class","message-remind-success").html(".xxx.").slideDown(400); in if (data == 'yes'){ $(this).show(); right here } - Ruslan Liashenka
  • @harbor is not quite clear what the problem is. What does not work if you insert your line of code? - Igor