Improve your skills

December 20, 2016

Show Bootstrap Modal Popup on Button Click in jQuery with Example


In this article, we will discuss about how to  show bootstrap modal popup on button click using jQuery with example and live demo.

Write the code as shown below-

HTML:
<body style="background: #E0EDFA; margin-top: 100px;">

    <div style="text-align: center; width: 100%;">
        <!--Button to Show Basic modal -->
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#basicModalPopup">Basic Modal</button>
        <!--Button to Show Large modal -->
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#largeModalPopup">Large Modal</button>
        <!--Button to Show Small modal -->
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#smallModalPopup">Small Modal</button>
    </div>

    <!-- Basic Bootstrap Modal Popup -->
    <div id="basicModalPopup" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="basicModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="basicModalLabel">Basic Modal Title</h4>
                </div>
                <div class="modal-body">
                    Welcome to <a href="http://webcodelogics.com" target="_blank">webcodelogics.com</a><br/>
                    
                    ...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Large Bootstrap Modal Popup -->
    <div id="largeModalPopup" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="largeModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="largeModalLabel">Large Modal Title</h4>
                </div>
                <div class="modal-body">
                    Welcome to <a href="http://webcodelogics.com" target="_blank">webcodelogics.com</a><br/>
                    We can make bootstrap modal larger by adding an extra class '.modal-lg' with class '.modal-dialog'.<br/>
                    ...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Small Bootstrap Modal Popup -->
    <div id="smallModalPopup" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="smallModalLabel">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="smallModalLabel">Small Modal Title</h4>
                </div>
                <div class="modal-body">
                    Welcome to <a href="http://webcodelogics.com" target="_blank">webcodelogics.com</a><br/>
                     We can make bootstrap modal smaller by adding an extra class '.modal-sm' with class '.modal-dialog'.<br/>
                    ...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save</button>
                </div>
            </div>
        </div>
    </div>

</body>


After that, attach the jquery library link, bootstrap jquery link and bootstrap css link in head part as shown below-


JS:

<head>
    <title>Show Bootstrap Modal Popup on Button Click in jQuery with Example</title> 
    <!-- jQuery Library Link -->
    <script src="https://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <!-- Bootstrap compiled and minified CSS Link -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <!-- Bootstrap compiled and minified JavaScript Link -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head>

Now save the file and run on browser to view the bootstrap modal popup result.

Preview:
show-hide-bootstrap-modal-popup-on-button-click-in-jquery-with-example-and-live-demo


Live Demo:








December 15, 2016

How to get browser name, version and other information in javascript with example


In this article, we will discuss about how to get the visitor's browser name, version and other information using window.navigator object in javascript.


get-browser-name-version-and-other-information-in-javascript-jquery-with-example

window.navigator have different properties which contains following information like-

Property Description
Browser Application Name navigator.appName Returns the application name of the browser.
Browser Version navigator.appVersion Returns version information of the browser
Browser Application Code Name navigator.appCodeName Returns the application code name of the browser.
Browser Engine Name navigator.product Returns the product name of the browser engine.
Browser Agent navigator.userAgent Returns the user-agent header sent by the browser to the server Returns the complete information of a browser like name, code name version etc.
Browser Platform navigator.platform Returns the platform of the browser (operating system).
Browser Language navigator.language Returns the language of the browser.
Is Browser Online? navigator.onLine Returns true if the browser is online, otherwise false.
Is Cookies Enabled? navigator.cookieEnabled Returns true if cookies are enabled in the browser, otherwise false.
Is Java Enabled? navigator.javaEnabled() Returns true if Java is enabled in the browser, otherwise false.
Write the code to get browser details as shown below-

HTML:
<body style="background: #E0EDFA; text-align: center; margin-top: 100px;">  
  <div id="dvBrowserInfo"></div>  
</body>

After that, attach the jquery library link and write the below javascript code in head part as shown below-

JS:
  
   Get Browser Name, Version and Other Information in javascript with Example  
    
    
  

Now save the file and run on browser to get all useful browser information.

Result (your browser detail):





October 05, 2016

Runtime Generate MS Word Document Report in ASP.NET C# or VB.NET with HTML


In this article, I will explain 'How We Dynamically Generate Word Document Report in ASP.NET with HTML'. For generate report we can uses many third party DLLs in asp.net but we can easily generate Ms Word document reports using HTML content. 

Here I will create a runtime HTML document using StringBuilder class which is very efficient for string manipulation. After that render the response as 'application/msword' mime type. 

The following example shown how to place heading and table dynamically in the word document in ASP.NET or VB.NET using HTML.

.aspx Page Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Runtime Generating MS Word Document Report in ASP.NET C# VB.NET with HTML (www.webcodelogics.com)</title>
    <style>
        table, td, th {
            border: 1pt solid #1FB5AD;
            font-family: arial;
        }
        td, th {
            padding: 2px 5px;
        }
    </style>
</head>
<body style="background: #E0EDFA; text-align: center; margin-top: 250px; font-family: Arial;">
    <form id="form1" runat="server">
        <div>
            <h2>Generate Word File</h2>
            <asp:Button ID="btnGenerateDocReport" runat="server" OnClick="btnGenerateDocReport_Click" ToolTip="Generate Word File" Text="Generate Word File"></asp:Button>
            <br />
            <br />
            <table cellspacing='0' cellpadding='0' width="200px" style="margin: auto;">
                <tr>
                    <th style='background: #1FB5AD; color: #fff;'>S.No.</th>
                    <th style='background: #1FB5AD; color: #fff;'>Name</th>
                </tr>
                <tr>
                    <td>1.</td>
                    <td>Harry</td>
                </tr>
                <tr>
                    <td>2.</td>
                    <td>John</td>
                </tr>
                <tr>
                    <td>3.</td>
                    <td>Martin</td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

C# Code:
// Add the following namespaces.
using System;
using System.Text;
using System.Web;

// Add the following code in the button click event.
protected void btnGenerateDocReport_Click(object sender, EventArgs e)
{
    StringBuilder strHtml = new StringBuilder();
 strHtml.Append("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
        strHtml.Append("<html xmlns='http://www.w3.org/1999/xhtml'>");
        strHtml.Append("<head><META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=UTF-8'>");
        strHtml.Append("<meta name=ProgId content=Word.Document>");
        strHtml.Append("<meta name=Generator content='Microsoft Word 9'>");
        strHtml.Append("<meta name=Originator content='Microsoft Word 9'>");
        strHtml.Append("<title>demoWordFile(webcodelogics.com)</title><style>");
        strHtml.Append("@page Section1 {size:595.45pt 841.7pt; margin:0.5in 0.5in 0.5in 0.5in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}");
        strHtml.Append("div.Section1 {page:Section1;}");
        strHtml.Append("@page Section2 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:0.5in 0.5in 0.5in 0.5in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}");
        strHtml.Append("div.Section2 {page:Section2;}");
        strHtml.Append("</style></head><body><div class=Section2>");

        strHtml.Append(" <table cellspacing='0' cellpadding='0' width='200' border='0' style='font-family: Open Sans,sans-serif;text-align:center;'> ");
        strHtml.Append(" <tr><th style='background:#1FB5AD;color:#fff;border:1pt solid #1FB5AD;'>S.No.</th><th style='background:#1FB5AD;color:#fff;border:1pt solid #1FB5AD;'>Name</th></tr> ");
        strHtml.Append(" <tr><td style='border:1pt solid #1FB5AD;'>1.</td><td style='border:1pt solid #1FB5AD;'>Harry</td></tr> ");
        strHtml.Append(" <tr><td style='border:1pt solid #1FB5AD;'>2.</td><td style='border:1pt solid #1FB5AD;'>John</td></tr> ");
        strHtml.Append(" <tr><td style='border:1pt solid #1FB5AD;'>3.</td><td style='border:1pt solid #1FB5AD;'>Martin</td></tr> ");
        strHtml.Append(" </table></div></body></html> ");

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = "application/msword";
        string strFileName = "demoWordFile(webcodelogics.com)" + ".doc";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + strFileName);
        HttpContext.Current.Response.Write(strHtml);
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Flush();
}

VB.Net Code:
// Add the following code in the button click event.
Protected Sub btnGenerateDocReport_Click(sender As Object, e As EventArgs)
    Dim strHtml As New StringBuilder() 

 strHtml.Append("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>")
        strHtml.Append("<html xmlns='http://www.w3.org/1999/xhtml'>")
        strHtml.Append("<head><META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=UTF-8'>")
        strHtml.Append("<meta name=ProgId content=Word.Document>")
        strHtml.Append("<meta name=Generator content='Microsoft Word 9'>")
        strHtml.Append("<meta name=Originator content='Microsoft Word 9'>")
        strHtml.Append("<title>demoWordFile(webcodelogics.com)</title><style>")
        strHtml.Append("@page Section1 {size:595.45pt 841.7pt; margin:0.5in 0.5in 0.5in 0.5in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}")
        strHtml.Append("div.Section1 {page:Section1;}")
        strHtml.Append("@page Section2 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:0.5in 0.5in 0.5in 0.5in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}")
        strHtml.Append("div.Section2 {page:Section2;}")
        strHtml.Append("</style></head><body><div class=Section2>")

        strHtml.Append(" <table cellspacing='0' cellpadding='0' width='200' border='0' style='font-family: Open Sans,sans-serif;text-align:center;'> ")
        strHtml.Append(" <tr><th style='background:#1FB5AD;color:#fff;border:1pt solid #1FB5AD;'>S.No.</th><th style='background:#1FB5AD;color:#fff;border:1pt solid #1FB5AD;'>Name</th></tr> ")
        strHtml.Append(" <tr><td style='border:1pt solid #1FB5AD;'>1.</td><td style='border:1pt solid #1FB5AD;'>Harry</td></tr> ")
        strHtml.Append(" <tr><td style='border:1pt solid #1FB5AD;'>2.</td><td style='border:1pt solid #1FB5AD;'>John</td></tr> ")
        strHtml.Append(" <tr><td style='border:1pt solid #1FB5AD;'>3.</td><td style='border:1pt solid #1FB5AD;'>Martin</td></tr> ")
        strHtml.Append(" </table></div></body></html> ")
         
        Dim fileName As String = "demoWordFile(webcodelogics.com)" + ".doc"
        Response.AppendHeader("Content-Type", "application/msword")
        Response.AppendHeader("Content-Disposition", "attachment; filename=" & fileName)
        Response.Write(strHtml.ToString())

End Sub

First clear the Response object, set charset to empty and set Content-Type to application/msword.
Define file name as per your choice for document generate through this above code and set this to "Content-Disposition" header of the Response object.
Set StringBuilder variable to Response object’s write.

Now, save the document and run it to your browser. 
Click the 'Generate Word File' button in the screen, Then prompt will be displayed to open or save the document.

Result:Runtime Generating MS Word Document Report in ASP.NET C# VB.NET with HTML



If you like this, please share with your friends...


September 27, 2016

How to Add or Remove HTML Element Dynamically in Javascript or JQuery


In this article, we will discuss about how to add or remove any html element dynamically in javascript or jquery.

We can easily add or remove any HTML elements like textbox, button, radio button etc. using JavaScript. JavaScript’s document object has a method called createElement() which can be used to create HTML elements dynamically.

First, we take a button(to add additional html element like textbox) or a textbox (default textbox) in body as show in below-

HTML:
<body style="background: #E0EDFA; text-align: center; margin-top: 250px;">
    <div id="dvAllTextbox">
        <input type="button" value="Add New Textbox" onclick="createTextbox();" /><br />
        <input type="text" /><br />
    </div>
</body>

After that, attach the jquery library link and write the below javascript code in head part as shown below-

JS:
<head>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">

        var i = 1;
        function createTextbox() {
            var div = document.createElement('DIV');
            div.innerHTML = "<input id='txt" + i + "' type='text' /><input id='btnRemove" + i + "' type='button' value='Remove' onclick='removeTextbox(this.id);' />"
            document.getElementById("dvAllTextbox").appendChild(div);
            i++;
        }

        function removeTextbox(id) {
            var newId = document.getElementById(id);
            newId.parentNode.parentNode.removeChild(newId.parentNode);
        }

    </script>
</head>

Now save the file and run on browser.

The function createTextbox() creates an HTML DIV element with a TextBox and a Button (to remove the TextBox) and then appends it to the Container DIV.

The function removeTextbox() simply removes the dynamically added DIV with TextBox and Button that was clicked.


Demo:






September 14, 2016

Add Simple Auto Complete Search to Input Box like Google using JQuery UI


In this tutorial, we learn how to implement auto-complete search textbox like google using jQuery UI. We can easily display a list of suggestions from the beginning of the word typed in a text box. Auto-complete prevents the user from having to enter an entire word or a set of words.

Here we have implemented a basic autocomplete with a local array as its data source. The source option is mandatory to which we are specifying a local array of strings. Once the user starting to enter country name, the auto suggested countries would be listed under the textbox. These auto suggested countries would be fetched from the list of the countries defined in our array.

First, we declare a input text box in <body> element as show in below-

HTML:
<body>
    <form>
        <input id="txtSearch" type="text" placeholder="Search Country" /><br/>
        <span id="lblMsg" style="color:green;"></span>
    </form>
</body>

After that, attach the jQuery library and jQuery UI link on your <head> part of page and write the below javascript code in head part shown as below-

JS:
<head>
<title>Add Simple Autocomplete Search to input box using jQuery UI</title>

<!-- Load jQuery, jQuery UI and jQuery ui styles from jQuery website -->
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<script type="text/javascript">

/* list of array of countries as its local data source*/
var data = ["Afghanistan", "Australia", "Bangladesh", "Canada", "China", "France", "Germany", "India", "Indonesia", "Iran", "Italy", "Japan", "Nepal", "New Zealand", "Pakistan", "Russia", "Saudi Arabia", "Sri Lanka", "United Arab Emirates", "United Kingdom", "Zimbabwe"];

$(document).ready( function () {
/* binding the text box with the jQuery Auto complete function. */
$("#txtSearch").autocomplete({
/*source refers to the list of countries that are available in the auto complete list. */
source:data,
autoFocus:true,
/*call this function after select value from autocomplete suggestions */
select: function (event, ui) {
$("#lblMsg").html("You selected: " + ui.item.label);
},
/*minLength defines, minimum character in textbox to show suggestions */
minLength: 0,
/*show autocomplete suggestions list on textbox focus.  */
}).focus(function () {
$(this).autocomplete("search", "");
});
});

</script>
</head>

Now, save and run the file in browser and when we start typing the first alphabet, it is matched against a list of the countries defined in our array and the matches get displayed in a drop-down menu attached to the textbox.

Result:


Live Demo (Try it):





If you like this, please share with your friends...



September 08, 2016

How to Disable Resizeable Property of Textarea


Normally whenever we use textarea in pages, the browser allows the user to resize it, but sometimes this is not required (as it affect the other part of the design).

So, In this article I will explain how to disable or prevent users from resizing the textarea element in browsers like Google Chrome, Mozilla Firefox and Apple Safari.

Note: By default, <textarea> elements are resizeable as shown below-.



----: Old Method :-----
In the old method, we need to set the value of the CSS property shown as below-
textarea { 
    width:400px; 
    max-width:400px; 
    height:300px; 
    max-height:300px; 
}
we set max-height/max-width same as height/width.
Example-

This method works well, but we still see "resize" icon in corner.

-----: New CSS3 Method :-----
We can use the CSS3 resize property to remove or disable the default horizontal and vertical resizeable property of the HTML <textarea> element. 

This property will also hide the resizing handle at the bottom-right corner of the textarea.
textarea {  
    resize:none;
}
Example-

Note: The resize property applies to elements whose overflow property value is something other than "visible".


-----: Optional / Conditional Resizing :-----

For enabling only the horizontal resizing, we can replace the none with horizontal.
textarea {  
    resize:horizontal;
}
Example-



Similarly for enabling only the vertical resizing, we can replace the none with vertical.
textarea {  
    resize:vertical;
}
Example-








September 03, 2016

How to Check weather a checkbox is checked or not using JavaScript or jQuery


Here I will explained with an example and live demo, How to check whether a CheckBox is checked (selected) or unchecked (not selected) using JavaScript or jQuery.

Description:

The checked property indicates whether a checkbox is checked or not. If a checkbox is selected, the value of checked property is true otherwise it is false.

----: Using JavaScript :-----

The following code consists an HTML CheckBox and a Button. When the Button is clicked, a click event handler is executed which first references the CheckBox using its ID and then based on whether it is checked or unchecked, displays a JavaScript alert message box.

Example-
<html> 
<head> 
    <script type="text/javascript"> 
        function Check() {
            var chkGraduate = document.getElementById("chkGraduate");
            if (chkGraduate.checked) {
                alert("CheckBox checked.");
            } else {
                alert("CheckBox not checked.");
            }
        } 
    </script> 
</head>
<body style="background: #E0EDFA;">
    <form id="form1">
        <input id="chkGraduate" type="checkbox" />
        <label for="chkGraduate">Are you graduate ?</label>
        <br />
        <br />
        <input type="button" value="Check" onclick="Check();" />
    </form>
</body>
</html> 



-----: Using jQuery :-----

There are two ways to track the status of checkboxes whether it is checked or not using the jQuery :checked selector and prop() method. We explained both methods one by one with example.

(1). Using the :checked Selector -

Using the jQuery ":checked" selector, we can easily check the status of checkboxes. The ":checked" selector specifically designed for radio button and checkboxes. If the checkbox is checked then it returns "true" otherwise it returns "false".

Example-
<html> 
<head> 
    <script src="https://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript"> 
        function Check() {
            var isChecked = $("#chkGraduate").is(":checked");
            if (isChecked) {
                alert("CheckBox checked.");
            } else {
                alert("CheckBox not checked.");
            }
        }
    </script> 
</head>
<body style="background: #E0EDFA;">
    <form id="form1">
        <input id="chkGraduate" type="checkbox" />
        <label for="chkGraduate">Are you graduate ?</label>
        <br />
        <br />
        <input type="button" value="Check" onclick="Check();" />
    </form>
</body> 
</html> 



(2). Using the prop() Method -

The jQuery prop() method provides an simple and reliable way to check the status of checkboxes. It works perfectly in all the conditions because every checkbox has checked property which specifie its checked or unchecked status.
If the checkbox is checked then prop() Method returns "true" otherwise it returns "false".

Note: Don't confuse with the "checked" attribute of checkbox. The "checked" attribute only define the initial state, not the current state of the checkbox.

Example-
<html> 
<head> 
    <script src="https://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript"> 
        function Check() {
            var isChecked = $("#chkGraduate").prop("checked"); 
            if (isChecked) {
                alert("CheckBox checked.");
            } else {
                alert("CheckBox not checked.");
            } 
        } 
    </script> 
</head>
<body style="background: #E0EDFA;">
    <form id="form1">
        <input id="chkGraduate" type="checkbox" />
        <label for="chkGraduate">Are you graduate ?</label>
        <br />
        <br />
        <input type="button" value="Check" onclick="Check();" />
    </form>
</body> 
</html> 

Result:
check weather a checkbox is checked or not in jquery or javascript


Live Demo:







August 29, 2016

Remove Special Characters From String Using Regular Expression (Regex) in JavaScript


In this article, we discuss about how to remove special characters (like !, #, $, %,  >, ?, :, #,@ etc.) from string in jQuery/JavaScript using Regular Expression (Regex). 

First, we take a input text or textarea and a input button in body as show in below-

HTML:
<body style="background: #E0EDFA; text-align: center; margin-top: 250px;">
<form id="form1">
<h1>Remove Special Characters From String Using Regular Expression (Regex) in JavaScript</h1>
<textarea id="txtString" rows="5" cols="50" placeholder=" write your text here"></textarea><br />
<input id="btnRemoveSpecialCharacter" type="button" value="Remove Special Characters" onclick="getSpeCharFreeText();" />
</form>
</body>

After that, attach the jquery library link and write the below javascript code in head part as shown below-

JS:
<head>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">

        function getSpeCharFreeText(id) {
            var txtString = $('#txtString').val().trim();
            var regExpr = /[^a-zA-Z0-9-. ]/g;
            var str = txtString.replace(regExpr, '')
            $('#txtString').val(str);
        }

    </script>
</head>

Now save the file and run on browser.

After write string (containing special characters) in textarea, click on 'Remove Special Characters' button. Then all the special characters(!, #, $, %,  >, ?, :, #,@ etc.) are removed and simple text content visible on the textarea.

Result:
Remove Special Characters From String Using Regular Expression (Regex) in JavaScript










Live Demo:



Subscribe for Latest Update

Popular Posts