Syed Abdul Baqi

Archive for the ‘JavaScript’ Category

jQuery is a JavaScript framework library, which makes the life of the web developer easier. In this tutorial I will focus on the implementation and uses of jQuery selectors. jQuery follows CSS style of referring to an element. Now I will try to give some list of examples below, which illustrates jQuery referring different kinds of elements.

HTML Code:

<div id="myDiv">Hello-1</div>
<div class="myDiv">Hello-2</div>
<div id="parentDiv"><div id="childDiv">Hello-3</div></div>
<div class="myDiv">Hello-4</div>
<div></div>

jQuery Code:

1. $("#myDiv")[0].innerHTML; returns "Hello-1"
2. $(".myDiv")[0].innerHTML; returns "Hello-2"
3. $("#childDiv")[0].innerHTML; returns "Hello-3"
4. $("#parentDiv > div")[0].innerHTML; returns "Hello-3"
5. $(".myDiv")[1].innerHTML; returns "Hello-4"
6. $("div")[0].innerHTML; returns "Hello-1"

These are just few basics examples for jQuery selector. There are many more ways in which the selectors can be used for accessing HTML elements. I will try to add more as I get time.

Comments are welcomed….

Advertisements

JavaScript function for converting string into Indian currency format

    //function for converting string into indian currency format
    function intToFormat(nStr)
    {
     nStr += '';
     x = nStr.split('.');
     x1 = x[0];
     x2 = x.length > 1 ? '.' + x[1] : '';
     var rgx = /(\d+)(\d{3})/;
     var z = 0;
     var len = String(x1).length;
     var num = parseInt((len/2)-1);
 
      while (rgx.test(x1))
      {
        if(z > 0)
        {
          x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        else
        {
          x1 = x1.replace(rgx, '$1' + ',' + '$2');
          rgx = /(\d+)(\d{2})/;
        }
        z++;
        num--;
        if(num == 0)
        {
          break;
        }
      }
     return x1 + x2;
    }

Hope this helps someone…..

Comments are welcomed….