Home > Blockchain >  Get value of select label html via custom attr
Get value of select label html via custom attr

Time:01-25

I want to know how to get with jquery with method val() the selected value of a select tag through a custom attribute inside it.

Example:

<select dta-tble="table1" onchange="printTable(event);">
var datatable = $(evt.target).attr("**dta-tble**");

var row = $(**..this the solution..**).val();

Thanks.

CodePudding user response:

try:

var value = $('select[**dta-tble**="table1"]').val();

I recomand you don't use ** characters on html attributed, use example data-table="table1" or id="table1" and you can get value more easly

CodePudding user response:

If you're using jQuery, dont use inline event handlers. You can target elements using the attribute selector

$("select[dta-tble]").on("change", function(){
    const val = $(this).val();
    console.log(val);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select dta-tble="table1">
  <option>1</options>
  <option>2</options>
  <option>3</options>
</select>

CodePudding user response:

The ** The page has added it automatically.

I want to avoid retrieving the value through the id id of the object. To retrieve it through custom attribute within it.

  •  Tags:  
  • Related