JQuery Tutorial 1: How to debug JQuery Code in the Google Chrome Browser

Introduction:

Using this JQuery Tutorial, we will learn how to debug Javascript or JQuery code in the Google Chrome browser.

Description:

In the Previous tutorials we have learnt How to debug javascript or jquery code in Internet Explorer and Mozilla firefox

Please watch Below video:

How to see Saved Passwords in Google Chrome Browser - Where saved password stored in Chrome

How to use Desktop Google Chrome browser as a Mobile Browser (Android, IOS or Black Barry)

How to use Google Chrome browser as Mobile browser (IPhone, Android etc...)


OPEN CHROME --> PRESS F12 --> GO TO RESOURCES TAB --> Click on settings image from right side down image ---> Go to overrides tab --> Check Enable check box --> check user agent check box --> select iphone 5 from dropdown list --> select device metrics and set up screen resolution(360x750) --> now browser will behave like a iOS Browser. 

How to copy pop up message to notepad in Windows

Click on pop up header and press ctrl+c after that open notepad and ctrl+v... You will see the message message in notepad.

How to do Check box check event dynamically?


Some times we may have some values are attaching to the checkbox ids, like example below. Using $("[Id*='isAccommadation']") code we can get selected checkbox value and we can do the event operations.

This is not limited to check box. This will applicable for all HTML elements.


<!DOCTYPE html>

<html>

<head>

    <title></title>

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

    <script language="javascript">

        $(document).ready(function() {



            $("[Id*='isAccommadation']").bind("click", function () {

                alert(this.value);

            });



        });

    </script>

</head>

    <body>

        <div id="hai">



        <input type='checkbox' name="1" id='isAccommadation_1' value="JQuery" />JQuery<br/>

        <input type='checkbox' name="2" id='isAccommadation_2' value="Example" />Example<br/>

        <input  type='checkbox' name="3" id='isAccommadation_3' value="Code" />Code<br/>

            </div>

    </body>

</html>


Check box checked and unchecked using JQuery

By using below code, if we check on one check box, all other check boxes will checked, if we un check all other check boxes will unchecked.


<!DOCTYPE html>

<html>

<head>

    <title></title>

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

    <script language="javascript">

        $('document').ready(function() {

            $('#requestAccommodication').bind("click", doOperations);



        });

        var doOperations = (function () {

            debugger;

            if ($('#hai #requestAccommodication').is(':checked')) {

                $("#hai input[type=checkbox]").each(function () {

                    $(this).prop("checked", true);

                });

            } else {

                $("#hai input[type=checkbox]").each(function () {

                    $(this).prop("checked", false);

                });

            }

        });

    </script>

</head>

    <body>

        <div id="hai">

        <input type="checkbox" id="requestAccommodication" /><br />

        <input type='checkbox' name="1" id='isAccommadation_1' />JQuery

        <input type='checkbox' name="2" id='isAccommadation_2' />Example

        <input  type='checkbox' name="3" id='isAccommadation_3' />Code

            </div>

    </body>

</html>

How to remove tag using JQuery but without tag inner content?

Remove tag using JQuery


We can remove HTML tag using jquery by different scenarios.

1. $('a').contents().unwrap();

This will remove the anchor tag but keep the anchor inner HTML.

2. Come to performance view below code is useful.

var b = document.getElementsByTagName('a');
while(b.length) {
    var parent = b[ 0 ].parentNode;
    while( b[ 0 ].firstChild ) {
        parent.insertBefore(  b[ 0 ].firstChild, b[ 0 ] );
    }
     parent.removeChild( b[ 0 ] );
}


3. We can also use replaceWith JQuery function. Please check below example.

$("a").replaceWith(function() { return $(this).contents(); });

If any other ways, please share in the comments section.

Is DOM element has been hidden or shown using jQuery?

Below JQuery samples will show you, how to find out hidden DOM element.


// Checks for display:[none|block], ignores visible:[true|false]
$(DOMElement).is(":visible")

//some more complete information
$(".item").each(function() {
if ($(this).css("visibility") == "hidden") {
// handle non visible state
} else {
// handle visible state
}
});
var isHidden = $('DOMElement').is(':hidden');

// Matches all elements that are hidden
$('DOMElement:hidden')

// Matches all elements that are visible.
$('DOMElement:visible')

//Functions don't work with the visibility attribute.
$(DOMElement).css('display') == 'none'

 

Redirect one webpage to other using Javascript/JQuery

There are so many ways to redirect one javascript file to other. As per my knowledge i am showing below ways, If any one knows any other way they can tell in the comments section.



// it won't store information about previous page.
window.location.replace("http://aspnettutorialonline.blogspot.com/");

// best way to use
window.location.href = "http://aspnettutorialonline.blogspot.com/";

//using javascript only.
window.location = "http://aspnettutorialonline.blogspot.com/";


//Using JQuery way - 1
var url = "http://aspnettutorialonline.blogspot.com/";   
$(location).attr('href',url);


//Using JQuery way  - 2.
$jq(window).attr("location","http://aspnettutorialonline.blogspot.com/");

//redirecting to the home page.
window.location = window.location.host

JQuery mouseenter(), hover(), mouseleave() Examples

Using below example we can do the mouse enter and mouse leave events using jquery.

Below example will show the inside list span tag text on mouse over.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <style type="text/css">
        ul
        {
            margin-left: 20px;
            color: blue;
        }
        li
        {
            cursor: default;
        }
        span
        {
            color: green;
            display: none;
        }
    </style>
    <script type="text/javascript">
        //first way to do one mouse over and mouse leave -- using toggle 
        $(function () {
            $(".category").hover(function () {
                $(this).find("span").toggle();
            });
        });
        //Second way to do mouseenter and mouse leave event. 
        $(function () {
            $(".category1").mouseenter(function () {
                $this = $(this);
                $this.find("span").css("display", "block");
            }).mouseleave(function () {
                $this = $(this);
                $this.find("span").hide();
            });
        });
    </script>
</head>
<body>
    <ul>
        <li class='category'>
            <span>aspnettutorialonline.blogspot.com</span>
        </li>
        <li class='category'><span>aspnettutorialonline.blogspot.com</span>
        </li>
        <li class='category'><span>aspnettutorialonline.blogspot.com</span>
        </li>
    </ul>
 
    <ul>
        <li class='category1'>
            <span>aspnettutorialonline.blogspot.com</span>
        </li>
        <li class='category1'><span>aspnettutorialonline.blogspot.com</span>
        </li>
        <li class='category1'><span>aspnettutorialonline.blogspot.com</span>
        </li>
    </ul>
</body>
</html>

Output:
 

How to implement validations on form controls using javascript before a postback occurs.


If we are using submit button in the form, we have to validation data before sending to the server. For this on submit we will call the javascript method. from the method we will return true or false based on the validation result.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        function Validate() {
            //Need to do validations here. 
            //If any thing fails we have to return false other wise request go to server
        }
    </script>
</head>
<body>
    <form onsubmit="return Validate()">
        ... all controls will go here with submit button...
    </form>
</body>
</html>


If you have any queries or suggestions, please feel free to ask in comments section.

Getting inside ul (list) hyper link id and name using JQuery

By using below example we can get the id and anchor tag name using JQuery.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(function () {
            debugger;
            var id = $('#links #active1').attr('id');
            var linkName = $('#links #active1 a').text();
            $('body').html('Id Name--------' + id + '<br />' + 'Name--------' + linkName);
            
        });
    </script>
</head>
<body>
    <ul id="links">
        <li id="active1"><a href="google.com" id="3">aspnettutorialonline.blogspot.com </a></li>
        <li id="active2"><a href="yahoo.com" id="2">Yahoo </a></li>
        <li id="Li1"><a href="facebook.com" id="A1">Facebook </a></li>
    </ul>
</body>
</html>

Output:

Getting inside ul hyper link id and name using JQuery
Getting inside ul hyper link id and name using JQuery
 

Applying css background property to inner div inside table using root class name

Using below code we can apply css into table div by using root class name.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('.rootClass table div').css('background', 'green');
        });
    </script>
</head>
<body>
    <div class="rootClass">
  <h3>Header</h3>
  <div>
     <p>this is para</p>
     <div>second para</div>
     <div>
         <table border="1">
           <tbody><tr><td><div>aspnettutorialonline.blogspot.com</div></td><td>THis is second column</td></tr></tbody>
         </table>
     </div>
   </div>
</div>
</body>
</html>
Output:


Applying css background property to inner div inside table using root class name
Applying css background property to inner div inside table using root class name
 

Access the objects inside another objects In the JSON Response Object using JQuery

Most of the AJAX response, we will get the JSON Resonse. Some times we may require to get the object details, inside other object. For this requirement, below code is useful.



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        var dataContainer = {
            metaData: {
                Paints: [{
                    id: 1,
                    color: { id: 123456 }
                },
                {
                    id: 2,
                    color: { id: 789 }
                }]
            }
        };
        $(function () {
            debugger;
            $.each(dataContainer.metaData.Paints,function(key,value){
                $('body').append($('<div>').html('<b>Id:</b>' + value.id + '----- <b> Color Code:</b>' + value.color.id));
            });
        });
    </script>
</head>
<body>
</body>
</html>

Output:

jQuery Tutorial - 100 - Other callback functions(Video Tutorial)

jQuery Tutorial - 99 - POST HTTP Request(Video Tutorial)

jQuery Tutorial - 98 - GET HTTP Request(Video Tutorial)

jQuery Tutorial - 97 - GET HTTP Request(Video Tutorial)

jQuery Tutorial - 96 - Load file(Video Tutorial)

jQuery Tutorial - 95 - Days until event(Video Tutorial)

jQuery Tutorial - 94 - now(Video Tutorial)

jQuery Tutorial - 93 - each(Video Tutorial)

jQuery Tutorial - 90 - Enabling checkbox after scrolling(Video Tutorial)

jQuery Tutorial - 92 - inArray(Video Tutorial)

jQuery Tutorial - 91 - Enabling checkbox after scrolling(Video Tutorial)

jQuery Tutorial - 89 - Scroll to top(Video Tutorial)

jQuery Tutorial - 88 - Minimum text field length(Video Tutorial)

jQuery Tutorial - 87 - Minimum text field length(Video Tutorial)

jQuery Tutorial - 86 - Placing DIV in very center of window(Video Tutorial)

jQuery Tutorial - 85 - Placing DIV in very center of window(Video Tutorial)

JQuery - Database simple tutorial

jQuery Tutorial - 84 - Adding to a dropdown menu(Video Tutorial)

Complete posts