Posted By


msstar on 12/07/12

Statistics


Viewed 472 times
Favorited by 0 user(s)

jQuery/JavaScript


/ Published in: jQuery
Save to your folder(s)

jQuery


Copy this code and paste it in your HTML
  1. //selected element if it's value equals to 0
  2. $("select option").filter(function(){
  3. if($(this).val() == "0") return $(this);
  4. }).attr('selected',true);
  5.  
  6. //difference between $(document).ready() and $(function(){});
  7. //they are both same, $(function(){}); is the short way of document ready
  8.  
  9. //double not operators (!!) changes a value into boolean type
  10. return !!isUTC //result is a boolean value
  11.  
  12. //double tilde operators (~~) used as a faster substitute for Math.floor()
  13. return ~~(3.4) //result is 3
  14.  
  15. //absolute round function
  16. function absRound(number) {
  17. if (number < 0) {
  18. return Math.ceil(number); //round upwards so -5.9 round to -5
  19. } else {
  20. return Math.floor(number); //round downwards so 5.9 round to 5
  21. }
  22. }
  23.  
  24. //to detect the type of object
  25. Object.prototype.toString.call(input) === '[object Array]'
  26. Object.prototype.toString.call(input) === '[object Number]'
  27. Object.prototype.toString.call(input) === '[object Math]'
  28. Object.prototype.toString.call(input) === '[object Date]'
  29. Object.prototype.toString.call(input) === '[object String]'
  30.  
  31. //=== vs. ==
  32. The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.
  33. The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. It's this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same.
  34.  
  35. // compare two arrays, return the number of differences
  36. function compareArrays(array1, array2) {
  37. var len = Math.min(array1.length, array2.length),
  38. lengthDiff = Math.abs(array1.length - array2.length),
  39. diffs = 0,
  40. i;
  41. for (i = 0; i < len; i++) {
  42. if (~~array1[i] !== ~~array2[i]) {
  43. diffs++;
  44. }
  45. }
  46. return diffs + lengthDiff;
  47. }
  48.  
  49. //expand-collapse text area
  50. <table>
  51. <tr>
  52. <td><div style="height:20px;"><textarea name="box" style="width: 100px; height:20px; background-color:white; position: relative; resize:none"></textarea></div></td>
  53. <td><div style="height:20px;"><textarea name="box" style="width: 100px; height:20px; background-color:white; position: relative; resize:none"></textarea></div></td>
  54. </tr>
  55. <tr>
  56. <td><div style="height:20px;"><textarea name="box" style="width: 100px; height:20px; background-color:white; position: relative; resize:none"></textarea></div></td>
  57. <td><div style="height:20px;"><textarea name="box" style="width: 100px; height:20px; background-color:white; position: relative; resize:none"></textarea></div></td>
  58. </tr>
  59. </table>
  60.  
  61. $('textarea[name=box]').focus(function()
  62. {
  63. /*to make this flexible, I'm storing the current width in an attribute*/
  64. $(this).attr('data-default', $(this).width());
  65. $(this).attr('height',$(this).height());
  66. $(this).animate({ width: 300,height:300}, 'slow').css("z-index",1);
  67. //$(this).height(300).width(300).css("z-index",1);
  68. }).blur(function()
  69. {
  70. /* lookup the original width */
  71. var w = $(this).attr('data-default');
  72. var h = $(this).attr('height');
  73. $(this).animate({ width: w, height:h }, 'slow').css("z-index",0);
  74. //$(this).height(h).width(w).css("z-index",0);
  75. });
  76.  
  77. //add date picker for all date fields dynamically
  78. $('body').on('focus','.date', function(){
  79. $(this).datepicker({dateFormat: "dd/mm/yy",changeMonth: true,changeYear: true});
  80. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.