Tampilkan postingan dengan label JavaScript. Tampilkan semua postingan
Tampilkan postingan dengan label JavaScript. Tampilkan semua postingan

Format Numbers as money in JavaScript

Posted On // Leave a Comment


Decimal Values
var profits=2489.8237
  profits.toFixed(3) //returns 2489.824 (round up)
  profits.toFixed(2) //returns 2489.82
  profits.toFixed(7) //returns 2489.8237000 (padding)
Thousand Separator
Number.prototype.formatMoney = function(c, d, t){
var n = this, 
    c = isNaN(c = Math.abs(c)) ? 2 : c, 
    d = d == undefined ? "." : d, 
    t = t == undefined ? "," : t, 
    s = n < 0 ? "-" : "", 
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
    j = (j = i.length) > 3 ? j % 3 : 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
 };
And use it with :
(123456789.12345).formatMoney(2, '.', ',');
If you're always going to use '.' and ',', you can leave them off your method call, the method will default them for you.
(123456789.12345).formatMoney(2);
If your culture has the two symbols flipped (i.e. Europeans), just paste over the following two lines in the formatMoney method:
 d = d == undefined ? "," : d, 
    t = t == undefined ? "." : t, 
sources : http://stackoverflow.com
[Read more]

create CSS class in JavaScript

Posted On // Leave a Comment


var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.cssClass { color: #F00; }';
document.getElementsByTagName('head')[0].appendChild(style);

document.getElementById('someElementId').className = 'cssClass';
[Read more]

setTimeout JavaScript

Posted On // Leave a Comment
setTimeout
function onFromCitySelected()
{
    isFromCitySelected=true;      
    document.getElementById('<%=btnGetContinentCountryFrom.ClientID %>').click();
    
    setTimeout
    (
        function()
        {
            if (document.getElementById('<%=lblMOP.ClientID %>').innerText == "MOPTM07/065A")
            {
                document.getElementById('<%=txtPortFromDetail.ClientID %>').value = "Tanjung Priok Port";
            }
        },
        1000
    );             
}
[Read more]

Checkboxlist Control Validations using Javascript

Posted On // Leave a Comment
var CHK = document.getElementById('<%=chkInterest.ClientID %>');
var checkbox = CHK.getElementsByTagName("input");
var counter=0;
for(var i=0;i<checkbox.length;i++)
    {
        if(checkbox[i].checked)
            counter++;
    }
alert(counter);
[Read more]

Dynamic File Upload ASP.NET Using JavaScript

Posted On // Leave a Comment

JavaScript

var counter = 0;
function AddFileUpload()
{
     var div = document.createElement('DIV');
     div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file" /><input id="Button' + counter + '" type="button" value="Remove" onclick = "RemoveFileUpload(this)" />';
     document.getElementById("FileUploadContainer").appendChild(div);
     counter++;
}
function RemoveFileUpload(div)
{
     document.getElementById("FileUploadContainer").removeChild(div.parentNode); 
}

ASPX

<form id="form1" runat="server" enctype="multipart/form-data" method = "post">
    <span style ="font-family:Arial">Click to add files</span>&nbsp;&nbsp;
    <input id="Button1" type="button" value="add" onclick = "AddFileUpload()" />
    <br /><br />
    <div id = "FileUploadContainer">
        <!--FileUpload Controls will be added here -->
    </div>
    <br />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
</form>

Code Behind

protected void btnUpload_Click(object sender, EventArgs e)
{
    for (int i = 0; i < Request.Files.Count; i++)
    {
        HttpPostedFile PostedFile = Request.Files[i];
        if (PostedFile.ContentLength > 0)
        {
            string FileName = System.IO.Path.GetFileName(PostedFile.FileName);
            PostedFile.SaveAs(Server.MapPath("Files\\") + FileName);
        }
    }
}
download source code :
 source : www.aspsnippets.com

[Read more]

Check String contains another substring in JavaScript

Posted On // Leave a Comment
var s = "foo";
alert(s.indexOf("oo") != -1);
[Read more]

Add Css Style Using Javascript

Posted On // Leave a Comment
document.getElementById("ctl00_ContentPanel_liPortFrom").style.border="1px solid red"
[Read more]

Change CSS Class Using Javascript

Posted On // Leave a Comment

Change Class

document.getElementById("ctl00_ContentPanel_txtPortFromCity").className = "error";

Append Class

document.getElementById("ctl00_ContentPanel_txtPortFromCity").className += " " + "error";
[Read more]

Sort Array of javaScript

Posted On // Leave a Comment

// Here's a more flexible version, which allows you to create 
// reusable sort functions, and sort by any field

var sort_by = function(field, reverse, primer){

   var key = function (x) {return primer ? primer(x[field]) : x[field]};

   return function (a,b) {
       var A = key(a), B = key(b);
       return ((A < B) ? -1 :
               (A > B) ? +1 : 0)) * [-1,1][+!!reverse];                  
   }
}


// Now you can sort by any field at will...

var homes = [{
   "h_id": "3",
   "city": "Dallas",
   "state": "TX",
   "zip": "75201",
   "price": "162500"
}, {
   "h_id": "4",
   "city": "Bevery Hills",
   "state": "CA",
   "zip": "90210",
   "price": "319250"
}, {
   "h_id": "5",
   "city": "New York",
   "state": "NY",
   "zip": "00010",
   "price": "962500"
}];

// Sort by price high to low
homes.sort(sort_by('price', true, parseInt));

// Sort by city, case-insensitive, A-Z
homes.sort(sort_by('city', false, function(a){return a.toUpperCase()}));
Fiddle : http://jsfiddle.net/dFNva/1/
source : http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects
[Read more]