JavaScript `this` keyword

Each time the function is called, this refers to the HTML element that is currently handling the event.

In JavaScript this always refers to the “owner” of the function we are executing, or rather, to the object that a function is a method of. When we define our  function doSomething() in a page, its owner is the page, or rather, the window object (or global object) of JavaScript. An onclick property, though, is owned by the HTML element it belongs to.

Advertisements

HTML5’s Data Attributes With jQuery

<html>
    <head>
        <title>Using HTML5's Data Attributes With jQuery</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                var val = $("#custom").data("emailid");
                if (val) {
                    $("#msg").html(val);
                }
            });
        </script>
    </head>
    
    <body>
        <div align="center" style="margin-top:100px;">
            <div>
                <h1>Using HTML5's Data Attributes With jQuery</h1>
            </div>
            <div>
                <input type="text" name="username" data-address="ahmedabad" data-name="Milap"
                data-emailid="cemilap.88@gmail.com" id="custom" />
            </div>
            <div>
                <span id="msg"></span>
            </div>
        </div>
    </body>

</html>

Setting up your favicon.ico

Sometimes it looks simple to Setup favicon icon for your website, but it is also quite useful when we start to set favicon as i do.

First , upload favicon.ico file in your root directory (most of times in public_html).

Just put below tag in your index file OR whichever common file for your project.


<link rel=”FaviconIcon” href=”favicon.ico” type=”image/x-icon” />

You are done..
This will display favicon icon in the starting of your Address Bar as well as in your current browser tab.
Although new browsers support GIF and PNG images , but , I would suggest always saving the image as favicon.ico.

Using Jquery – Remove Space and special characters while typing

Whenever you need to remove “space” as well as “special characters” between characters while typing, you can do it easily using jQuery as described below :-

Checkout this code in action :- http://jsfiddle.net/hn9wk/18/

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<input type="text" name="text_box_id" id="text_box_id" />
<script>
    $("#text_box_id").change(function() {
        var name = $(this).val();
        var dname_without_space = $("#text_box_id").val().replace(/ /g, "");
        var name_without_special_char = dname_without_space.replace(/[^a-zA-Z 0-9]+/g, "");
        $(this).val(name_without_special_char);
    });
</script>
</html>