jQuery

Much thanks to John Resig!

03/> get number of characters in a JSON response

the json payload it will be wrapped inside ‘pre’ tag when the browser renders it.

# in plain old JS
document.getElementsByTagName("pre")[0].innerHTML.length

02/> remove all child nodes of a div

//js
$( "button" ).click(function() {
  $("#container").empty();
});

<div id="container" class="abc">
  <div class="hello">Hello</div>
  <div class="goodbye">Goodbye</div>
</div>
<button>Clear</button>

01/> numeric textfield

$("#txtNumeric")
 .on('keydown', function(e) {

  if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 || 
   (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) || 
   (e.keyCode == 67 && (e.ctrlKey === true || e.metaKey === true)) || 
   (e.keyCode == 88 && (e.ctrlKey === true || e.metaKey === true)) || 
   (e.keyCode >= 35 && e.keyCode <= 39)) {
    console.log(e.keyCode);
    return;
  }

  // Ensure that it is a number and stop the keypress
  if ((e.shiftKey || (e.keyCode  57)) && 
   (e.keyCode  105)) {
    e.preventDefault();
   }
  });
<input type="text" id="txtNumeric" />
Advertisements