php - Trying to get the value of message from any of the clicked form or button? Any help will be appreciated -


the varibale message in script returning undefined while variable name true... want input text name of button clicked

<script type="text/javascript"> $(function() {     $(".submitcc").click(function() {         var name = $(this).attr("name");         var message = $("#" + name).val();         return false;     }); }); </script> 

this form calling script above...

<form method="post" action="">     <input type="text" name="message" id="submitcc_2_2" />     <button class="submitcc"name="submitcc_2_2">send</button> </form>  <form method="post" action="">     <input type="text" name="message" id="submitcc_5_2" />     <button class="submitcc"name="submitcc_5_2">send</button> </form> 

is there wrong?

the scope of message in function only.

you have use $('input[name="message"]') message input.

you can use

var message = $('input[name="message"]').val(); 

where need message

if have multiple forms on page:

$(function () {     var message = '';      $(document).on('click', ".submitcc", function () {         message = $("#" + $(this).attr("name")).val();          return false;     });      // can use message here }); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -