Thursday, July 3, 2014

Rounding Numbers to ‘N’ Decimals in javascript

var num = 2.443242342;
alert(num.toFixed(2)); // 2.44

num = 500.2349;
result = num.toPrecision(4); // result will equal 500.2

Initialize JavaScript Array with series of numbers

<button onclick="init()">Initialize array</button>
<script>
function init() {
    var numbers = [];
    for(var i=1; numbers.push(i++)<20;);

    alert(numbers);
}
</script>

Listbox Move Left/Right Options using javascript

<table>
<tbody><tr>
    <td>
<select id="sourceSelect" size="10" multiple="">
 <option value="a">Afghanistan</option>
 <option value="b">Bahamas</option>
 
 <option value="d">Belgium</option>
 <option value="e">Bhutan</option>
 <option value="f">China</option>
 <option value="g">Croatia</option>
 <option value="h">Denmark</option>
 <option value="i">France</option>
</select>
    </td>
    <td>
        <button onclick="listboxMoveacross('sourceSelect', 'destSelect');">&gt;&gt;</button>  <br>
        <button onclick="listboxMoveacross('destSelect', 'sourceSelect');">&lt;&lt;</button>
    </td>
    <td>
<select id="destSelect" size="10" multiple="">
 <option value="a">Afghanistan</option>
 <option value="b">Bahamas</option>
 <option value="c">Barbados</option>
 <option value="d">Belgium</option>
 <option value="e">Bhutan</option>
 <option value="f">China</option>
 <option value="g">Croatia</option>
 <option value="h">Denmark</option>
 <option value="i">France</option>
<option value="c">Barbados</option></select>
    </td>
</tr>
</tbody></table>

    <script>
function listboxMoveacross(sourceID, destID) {
    var src = document.getElementById(sourceID);
    var dest = document.getElementById(destID);

    for(var count=0; count < src.options.length; count++) {

        if(src.options[count].selected == true) {
                var option = src.options[count];

                var newOption = document.createElement("option");
                newOption.value = option.value;
                newOption.text = option.text;
                newOption.selected = true;
                try {
                         dest.add(newOption, null); //Standard
                         src.remove(count, null);
                 }catch(error) {
                         dest.add(newOption); // IE only
                         src.remove(count);
                 }
                count--;
        }
    }
}
</script>

Listbox Move selected items Up / Down using javscript

 <select id="lsbox" name="lsbox" size="10" multiple="">
        <option value="1">India</option>
        <option value="2">United States</option>
        <option value="4">Italy</option>
        <option value="3">China</option>
        <option value="5">Germany</option>
        <option value="6">Canada</option>
        <option value="7">France</option>
        <option value="8">United Kingdom</option>
    </select> <br>
<button onclick="listboxMove('lsbox', 'up');">Move Up</button>
<button onclick="listboxMove('lsbox', 'down');">Move Down</button>
<script>
function listboxMove(listID, direction) {

    var listbox = document.getElementById(listID);
    var selIndex = listbox.selectedIndex;

    if(-1 == selIndex) {
        alert("Please select an option to move.");
        return;
    }

    var increment = -1;
    if(direction == 'up')
        increment = -1;
    else
        increment = 1;

    if((selIndex + increment) < 0 ||
        (selIndex + increment) > (listbox.options.length-1)) {
        return;
    }

    var selValue = listbox.options[selIndex].value;
    var selText = listbox.options[selIndex].text;
    listbox.options[selIndex].value = listbox.options[selIndex + increment].value
    listbox.options[selIndex].text = listbox.options[selIndex + increment].text

    listbox.options[selIndex + increment].value = selValue;
    listbox.options[selIndex + increment].text = selText;

    listbox.selectedIndex = selIndex + increment;
}
</script>

Listbox Select All/Deselect All using JavaScript

<select id="lsbox" name="lsbox" size="10" multiple="">
        <option value="1">India</option>
        <option value="2">United States</option>
        <option value="3">China</option>
        <option value="4">Italy</option>
        <option value="5">Germany</option>
        <option value="6">Canada</option>
        <option value="7">France</option>
        <option value="8">United Kingdom</option>
    </select> <br>
<button onclick="listboxSelectDeselect('lsbox', true);">Select All</button>
<button onclick="listboxSelectDeselect('lsbox', false);">Deselect All</button>
<script>
function listboxSelectDeselect(listID, isSelect) {
    var listbox = document.getElementById(listID);
    for(var count=0; count < listbox.options.length; count++) {
            listbox.options[count].selected = isSelect;
    }
}
</script>

Deleting Multiple Values From Listbox in JavaScript

    <select id="lsbox" name="lsbox" size="10" multiple="">
        <option value="1">India</option>
        <option value="2">United States</option>
        <option value="3">China</option>
        <option value="4">Italy</option>
        
        <option value="6">Canada</option>
        <option value="7">France</option>
        <option value="8">United Kingdom</option>
    </select> <br>
<button onclick="selectBoxRemove('lsbox');">Delete Selected Options</button>
<script>
function selectBoxRemove(sourceID) {

    //get the listbox object from id.
    var src = document.getElementById(sourceID);

    //iterate through each option of the listbox
    for(var count= src.options.length-1; count >= 0; count--) {

         //if the option is selected, delete the option
        if(src.options[count].selected == true) {

                try {
                         src.remove(count, null);

                 } catch(error) {

                         src.remove(count);
                }
        }
    }
}

</script>

Disable Back button using JavaScript

<SCRIPT type="text/javascript">
    window.history.forward();
    function noBack() { window.history.forward(); }
</SCRIPT>
</HEAD>
<BODY onload="noBack();"
    onpageshow="if (event.persisted) noBack();" onunload="">