wawan Setiyawan wawan Setiyawan - 1 month ago 5
HTML Question

Get this value by class on onclick

i have form and jquery for get value

this
by class



function sendModel(el){
var id_model = $(el).class('id_model');
alert(id_model);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input name='id_model' class='id_model' value='M001'>

<button onclick='sendModel(this)'> send </button>

</form>





how to show alert with
id_model
value ??thanks guys

Answer

Submit form

$('#myForm').submit(function(e){
  e.preventDefault();
  var id_model = $('.id_model').val();
  alert(id_model);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
  <input name='id_model' class='id_model' value='M001'>
  
  <button type="submit"> send </button>
  
  </form>

Your way

function sendModel(){
  var id_model = $('.id_model').val();
  alert(id_model);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input name='id_model' class='id_model' value='M001'>
  
  <button onclick='sendModel()'> send </button>
  
  </form>

Comments