Question: How to replace the text of div using class?
Suppose HTML
jQuery code Update this text
$('.myDivClass').text('Updated');
Question: How to replace the text of div using "div id"?
Suppose HTML
jQuery code Update this text
$('#myDivId').text('Updated');
Question: How to replace the html of div using class?
Suppose HTML
jQuery code Update this html
$('.myDivClass').html('Updated');
Question: How to replace the text of div using "div id"?
Suppose HTML
jQuery code Update this text
$('#myDivId').html('Updated');
Question: How to Check if checkbox is checked?
Suppose HTML is
JQuery code
var totalCheckboxSelected = $('input[name="lang[]"]:checked').length;
if(totalCheckboxSelected){
console.log(totalCheckboxSelected+' check box checked');
}else{
console.log('NO checkbox is selected');
}
Question: How can you check for a #hash in a URL?
if(window.location.hash) {
var hashValue = window.location.hash.substring(1);
console.log(hashValue);
} else {
console.log('Hash not found');
}
Question: How get to know which mouse button is pressed?
$('#divId').mousedown(function(event) {
switch (event.which) {
case 1:
console.log('Left button pressed.');
break;
case 2:
console.log('Middle button pressed.');
break;
case 3:
console.log('Right button pressed.');
break;
default:
console.log('Exception Case!');
}
});
Question: How to add update the color of div with jQuery?
$('div#divId').css("background-color", "red");
Question: How to remove class from div?
$("div#divId").removeClass('className');
Question: How do I get the value of a textbox using jQuery?
Html
$("#websiteId").val();
Question: How to submit form with post method with Ajax?
HTML Form
jQuery Code
$( document ).ready(function() {
$("#ajaxformSubmit").submit(function(e)
{
var postData = $(this).serializeArray();
var formURL = '/ajax/form-url'
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
success:function(data, textStatus)
{ console.log(data);
}
});
e.preventDefault(); //STOP default action
});
});
Question: How to escape the text?
var someHtmlString = "";
var escaped = $("
").text(someHtmlString).html();
console.log(escaped); //<script>aalert('hi!');</script>
