Reading a Checkbox/Radio Button/Select option Value in JQuery and Enter Key press fire event

Reading a Checkbox/Radio Button/Select option Value in JQuery and Enter Key press fire event
var getdirectmessage = false;
if( jQuery('#message').is(':checked')  ){
    var getdirectmessage = true;
}else{
    var getdirectmessage = true;
}

HTML Code


screen51

screen52

Something Similar can be done for A radio Button as well code copied from





jQuery Get Selected Radio Button Value


 

    

Please select your gender.

Now when select boxes come into place the following code can be used to read their value in java script the first one is used to select the selected options data attribute and other one is used to get the value of selected option

var category = jQuery("#category option:selected").data("tid");
var industry = jQuery("#SelectDropdown2 option:selected").val();

Following code is used to Fire and Enter Key Event

jQuery(document).keydown(function(e) { 
  if (e.which === 13) {
  reset();
  var searchtext = jQuery('#searchtext').val();
	var pageno   = jQuery('#pageno').val();
	var category = jQuery("#category option:selected").data("tid");
	var industry = jQuery("#SelectDropdown2 option:selected").val();
	var location = jQuery("#SelectDropdown1 option:selected").val();
	var data = { 'action': 'filter_product',searchtext, category, industry, location, pageno };
	jQuery.ajax({
         	url    : ajaxurl,
         	type   : 'POST',
         	data   : data,      
         	beforeSend: function() {
              
          },         
         	success: function(response) {
         		jQuery('#postsgrid').empty().append(response);
         		jQuery('#pageno').val( parseInt(jQuery('#pageno').val()) +1 );
         	},
      	});
    }
});

Suppose you want to select all values as string for more than one check boxes or radio button group you can use following code

function brand(){
          let checkedValues1 = '';
          jQuery('#multiSelectDropdown1 input').each(function(){
               if(jQuery(this).is(':checked')){
                    checkedValues1 += jQuery(this).val() + ',';
               }
          });
          return checkedValues1 = checkedValues1.trim();
     }

Leave a Reply

Your email address will not be published. Required fields are marked *