View on GitHub

JQuery Snippets

I can never remember how to do bits and bobs in Jquery, so here they are:

Set a value



            type: 'POST', // define the type of HTTP verb we want to use (POST for our form)
            url: '/records/store', // the url where we want to POST
            headers: {
                'X-CSRF-TOKEN': ''
            data: formData, // our data object
            dataType: 'json', // what type of data do we expect back from the server
            encode: true,
            success: function (data) {
                // success logic
                $("#panel-head").notify("Record Added","success");

            error: function (data) {
                var errors = data.responseJSON;
                console.log(errors); //{"success":"false","0":{"part_no":"Not a valid product!"}}


Retrive a number from an element

function getNum(val) {
    if (isNaN(val)) {
        val= 0;

    if (val=="") {
        val =  0;

    return parseInt(val);

Select the text when the element receives focus

$(document).ready(function () {
    $("input").focus(function () {

Add class


Enable/Disable Button

$(“#create”).attr(‘disabled’,’disabled’); $(“#create”).removeAttr(‘disabled’);

Lost Focus

$( “.typeahead” ).blur(function() { }