`$('el').val().length`
$.ajax({
url: "test.html",
cache: false,
success: function(html){
$("#results").append(html);
}
});
$.ajax({
url: "test.html",
cache: false,
type: "POST",
data: {id : menuId}
success: function(html){
$("#results").append(html);
}
});
Optionally do other stuff:
...
request.done(function(msg) {
$("#log").html( msg );
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
$(document).ready(function() {
//stuff
});
Jquery.noConflict();
removes the $() shortcut and replaces with Jquery()
$('.checkbox').click (function(){
var thisCheck = $(this);
if ( thischeck.is(':checked') ) {
// Do stuff
}
});
$('#form_id').on('submit',function (e) {
e.preventDefault();
// do more stuff
});
$('#form2').find('input[name=name]');
$('#form2').find('#email');
var bla = $('#txt_name').val();
$('#txt_name').val('bla');
$('#inputname').focus();
What's happening? We're binding to #form_container which does exist, then delate to #form_id to trigger when that does exist)
$('#form_container').on('submit','#form_id', function (e)) {
e.preventDefault();
// do more stuff
});
$().jquery; or $.fn.jquery;
<p class="a b"> $('.a.b') this is an inersection
<p id="z" class="a b"> $('#z.a.b') this is an inersection
<p class="a"><p class="b"> $('.a .b') search for a and then any descendents with .b
OR
$('.a').filter('.b'); this is slower as it builds a list of objects with class a first and then
$.get('/url/', function(data){
$(data).find("#id_to_find").appendTo("#id_to_append");
});
e.preventDefault();
e.stopPropagation();
What's happening? stopPropagation stops the event from bubbling up the event chain, and preventDefault prevents the default action the browser makes on that event.
function() {
return false;
}
// IS EQUAL TO
function(e) {
e.preventDefault();
e.stopPropagation();
}
if( $('#leftmenu').is(':empty') ) if( $('#leftmenu:empty').length )
html
<div id="mydiv" data-myval="10"></div>
get the data
var a = $('#mydiv').data('myval'); //getter
set the data
$('#mydiv').data('myval',20); //setter
| What | How | |
|---|---|---|
| Selecting tag name | $("li").text("new list item") |
<ol><li></li><li></li></ol> |
| class selector | $(".class").text("new text") |
|
| Id selector | $('#id').text('new text') |
|
| Add class | $(".class").css("display","block"); |
|
| Remove Class | $(".class").css("display","none"); |
|
| Replacing Html | $( "div.demo-container" ).html(); |
|
| Referencing textara | content_textarea.val() (don't use content_textarea.text() as that will load the oriiginal text |
|
| Get glassname of div | $('.myclass').attr('class'); | |
| Remove class | $('#mydiv').removeClass('colors'); | |
| Remove all classes | $('#mydiv').removeClass(); |
http://refcardz.dzone.com/refcardz/jquery-selectors
To prevent cross-site request forgery you need to add a token to the html page to prevent a malicious website submitting a form
$.ajaxSetup({ headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') } });
Request::header('x-csrf-token');
$.getScript('/path/to/imported/script.js', function()
{
// script is now loaded and executed.
// put your dependent JS here.
// what if the JS code is dependent on multiple JS files?
});
$('[id^=pick_]').click(
function(event) {
// Do something with the id # here:
alert('Picked: '+ event.target.id);
// $(this).attr("id")
}
);