javascript - How to pass a jquery value in a div or form and assign it in a hidden textbox? -


just want ask question passing value using jquery , assigning in form hidden textbox. here's code hope can me.

**homepage.php**  <?php     $cat_id = $row['salescatid'];  ?>  <input type="button" name="add_comment" data-value="<?php echo $cat_id; ?>" value="add comment" class="comment" />  .  .  <div id="comment_category">  <!-- display comment here -->  </div>  <?php echo form_open('category_controller/insert_comment'); ?>  <div id="comment_add" style="display: none;">     <input type="hidden" value="" name="cat_id" /> //here's problem how can value jquery?     <input type="hidden" value="sales_category" name="type"/>     <label>write comment</label><br />     <textarea name="comment_category" style="width: 50%; resize: none;"></textarea><br />     <input type="submit" value="comment" class="btn btn-primary btn-small"/>  </div>  <?php echo form_close(); ?>  .  .  .  //here's jquery  $(".comment").click(function(){        var id = $(this).attr('data-value'); //this value of button, problem how pass in comment form?        $("#comment_add") .show();        //alert(id);  }); 

please me guys. thanks.

you can use .val() assign value input element , use attribute selector select input field using name attribute

$('input[name="cat_id"]').val($(this).attr('data-value')) 

ex:

 $(".comment").click(function(){        var id = $(this).attr('data-value'); //this value of button, problem how pass in comment form?        $('input[name="cat_id"]').val(id)        $("#comment_add") .show();        //alert(id);  }); 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -