Skip to content

Instantly share code, notes, and snippets.

@Jaballadares
Last active August 29, 2015 14:16
Show Gist options
  • Save Jaballadares/c97533fb1f3e925e6290 to your computer and use it in GitHub Desktop.
Save Jaballadares/c97533fb1f3e925e6290 to your computer and use it in GitHub Desktop.
JQuery Snippets
$.ajax({
type: 'POST',
url: 'backend.php',
data: "q="+myform.serialize(),
success: function(data){
// on success use return data here
},
error: function(xhr, type, exception) {
// if ajax fails display error alert
alert("ajax error response type "+type);
}
});
$.ajax({
type: 'POST',
url: 'backend.php',
data: "q="+myform.serialize(),
success: function(data){
// on success use return data here
},
error: function(xhr, type, exception) {
// if ajax fails display error alert
alert("ajax error response type "+type);
}
});
var sometext = "here is more HTML";
$("p#text1").append(sometext); // added after
$("p#text1").prepend(sometext); // added before
var maxheight = 0;
$("div.col").each(function(){
if($(this).height() > maxheight) { maxheight = $(this).height(); }
});
$("div.col").height(maxheight);
$('input').keydown(function(e) {
// variable e contains keystroke data
// only accessible with .keydown()
if(e.which == 11) {
e.preventDefault();
}
});
$('input').keyup(function(event) {
// run other event codes here
});
$("#content").load("somefile.html", function(response, status, xhr) {
// error handling
if(status == "error") {
$("#content").html("An error occured: " + xhr.status + " " + xhr.statusText);
}
});
$("p").click(function () {
var htmlstring = $(this).html(); // obtain html string from paragraph
$(this).text(htmlstring); // overwrite paragraph text with new string value
});
var value1 = $('input#username').val(); // textfield input value
var value2 = $('input:checkbox:checked').val(); // get the value from a checked checkbox
var value3 = $('input:radio[name=bar]:checked').val(); // get the value from a set of radio buttons
var alink = $("a#user").attr("href"); // obtain href attribute value
$("a#user").attr("href", "http://www.google.com/"); // set the href attribute to a new value
$("a#user").attr({
alt: "the classiest search engine",
href: "http://www.google.com/"
}); // set more than one attribute to new values
$('nav a').toggleClass('selected');
$("a.register").on("click", function(e){
$("#signup").fadeToggle(750, "linear");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment