Useful javascripts function

This is some simple jQuery function i used for my pets project. Check it out

[googlefont font=”Enriqueta” fontsize=”30″]Empty Check[/googlefont]
Return true if the value is empty

function emptyCheck( value ) {
	return ( value || '') ? true : false;
}

 

[googlefont font=”Enriqueta” fontsize=”30″]Multiple Selector[/googlefont]
This function to join 2 selected $(tr.g6) and $(tr.g7) and filter by input

$("input", $('tr.g6, tr.g7')).keyup(function() {
  var oEmpty = emptyCheck('test') || notEmpty('test2');
  disableGroup('tr.g1', oEmpty);
});

 

[googlefont font=”Enriqueta” fontsize=”30″]Filter not empty[/googlefont]
This function is used to filter the input textbox where name = amount and it is not empty.

function getNotEmptyInputField() {
   return $('input[name=amount]', $('tr')).filter( function (index) {
      return ( this.value || '') ? this.value : false;
   });
}

 

[googlefont font=”Enriqueta” fontsize=”30″]JQuery this[/googlefont]
this – syntax in jquery function event listener, it will refer to javascript this. If you want it refer to jQuery current active element you’d need to change to $(this). Example we have selected few button for onclick event.

$("button").on("click", function() {
   $(this).addClass("highlight");
});

 

[googlefont font=”Enriqueta” fontsize=”30″]Javascript function or trigger function[/googlefont]
If you defined a javascript function when you’d like to call it immediately you need to include parentheses. If you’d like to get call by trigger function just exclude the parentheses. Example.

function doNow() {
   alert('do something now');
}

function doLater() {
   alert('do something else later');
}

$(document).ready(function() {
    $(".now").on("click", doNow());

 

[googlefont font=”Enriqueta” fontsize=”30″]Integer check[/googlefont]

function isInteger(text) {
    return /^\d+$/.test(value);
}

 

[googlefont font=”Enriqueta” fontsize=”30″]Type cast value (string) to integer[/googlefont]

var amount = +$('#amount').val();
var quantity = +$('#quantity').val();
var total = amount + quantity;

 

Useful javascripts function

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.