Skip to content

Instantly share code, notes, and snippets.

@muthu32
Last active July 4, 2019 08:18
Show Gist options
  • Save muthu32/55f170f4fbd8cc8ba1deb3b7b10eed55 to your computer and use it in GitHub Desktop.
Save muthu32/55f170f4fbd8cc8ba1deb3b7b10eed55 to your computer and use it in GitHub Desktop.
//Select class selector
//select option by index
$('#frakt_type option:eq(0)').prop("selected",true);
//select option by value
$('#frakt_type option[value="'+4+'"]').prop("selected",true);
//Select option by contains
$('#frakt_type option:contains("find_text")').prop("selected",true);
A match on "Selection 10" might be unwanted. I found no selector to match the full text, but a filter works:
$('.selDiv option').filter(function(i, e) { return $(e).text() == "Selection 1"})
//Get option value
$('#frakt_type option:selected').val();
//remove option by data index
$('#frakt_type option[data-id="'+4+'"]').remove();
//add options to select
var option = '<option value="'+val+'" data-id="'+frakt_inc+'f">'+text+'</option>';
$("#frakt_type").append(option);
//change data attributes in select
$('#frakt_type option[data-id="'+$id+'"]').data("id",frakt_inc+"f");
$('#frakt_type option[data-id="'+$id+'"]').attr("data-id",frakt_inc+"f");
//Remove All Options Except The First
$('select').children('option:not(:first)').remove();
//Add options from array
$.each(sel_array, function(key, value) {
$('#mySelect')
.append($("<option></option>")
.attr("value",key)
.text(value));
});
//Check not numeric
if(!$.isNumeric($id)) {
// do something
}
//Change HTML TEXT
var html = $('#offertHtml').html();
var $html = $('<div />',{html:html});
$html.find('#box').css({"box-shadow":''});
$('#html_offert').text($html.html());
//Ajax method
//use data:{data:data} to send object
$.ajax({
url: "ajax_file.php",
type: "post", //or get
//data: fdata, //(or)
data: {
lang: $('#lankade').val()
},
//dataType: "json", //only get Json File
success: function(data)
{
alert(data);
},
error: function(error)
{
alert(error);
}
});
//prevent event in a form.
$(this).preventDefault();
//Get Ready state
$( document ).ready(function() {
console.log( "document loaded" );
});
$( window ).on( "load", function() {
console.log( "window loaded" );
});
//Dynamic add event listener
$('#staticDiv').on('click', 'yourSelector', function() {
//do something
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment