Header

Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Monday, 1 May 2023

How to prevent to fire parent click in jQuery/JavaScript

Hi Friends,
Today we will see how we can prevent parent click event when end user clicks on child. 

Some time it happens that we have associated click event of parent DOM element and also on click of Child DOM element. In this situation if end user clicks on Child element, then both events will be triggered. But our requirement is not firing Parent event on clicking on child element. 
<html>
    <head>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"
  integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8="
  crossorigin="anonymous"></script>
  <script>
    $(document).ready(function() {
    $(".parent").on('click',function(e){
        alert("You have called Dad");
    });
    $(".child").on('click',function(e){
          alert("You have called son");
    });
});
  </script>
    </head>
    <body>
        <div class="parent">
            <div class="child">Child where event has been added</div>
            <div>Child where no event has added </div>
        </div>
    </body>
</html>

If you run above code, it will show you two alerts on clicking on Child element. 
To resolve this issue, we will use e.stopPropagation();
This small line of code will solve the problem. 


<html>
    <head>
<script
  src="https://code.jquery.com/jquery-3.6.4.min.js"
  integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8="
  crossorigin="anonymous"></script>
  <script>
    $(document).ready(function() {
    $(".parent").on('click',function(e){
        alert("You have called Dad");
    });
    $(".child").on('click',function(e){
        e.stopPropagation();
        alert("You have called son");
    });
});
  </script>
    </head>
    <body>
        <div class="parent">
            <div class="child">Child where event has been added</div>
            <div>Child where no event has added </div>
        </div>
    </body>
</html>











Friday, 26 October 2012

find checked radio button throug jQuery

$("#_chkbox").change(function () {
            var items = $(".childCheckBox").find("input");
            if ($('#_chkbox').is(':checked') == false) {
                for (i = 0; i < items.length; i++) {
                    if (items[i].type == "checkbox") {
                        items[i].checked = false;
                    }
                }
            }
            if ($('#_chkbox').is(':checked') == true) {
                for (i = 0; i < items.length; i++) {
                    if (items[i].type == "checkbox") {
                        items[i].checked = true;
                    }
                }
            }
        });

Wednesday, 23 May 2012

How to identify Browser using Javascript

Hi Friends,

We will see how we can get the client browser information.

Let see an example, like if we want to know is this browser Chrome or not then we can

In JavaScript we have object which will let us know the information of client browser.

//navigator.userAgent


 if( navigator.userAgent.toLowerCase().indexOf('chrome')>-1)
{
alert("it is chrome");
} 
else
{
alert("it is not Chrome");
}

In the above we are checking the name of Browser in the same way you can find the name.






Thursday, 20 October 2011

Get Query String Value

How to get value from QueryString.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function querySt(elementName) {
                curulr = window.location.search.substring(1);
                qryElement = curulr.split("&");
                for (i=0;i< qryElement.length;i++) {
                                spltdElement = qryElement [i].split("=");
                                                if (spltdElement[0] == elementName) {
                                                return spltdElement[1];
                                                }
                                }
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
This is very small function but very power full. You need to pass query string element name and if it is in query string it will return the value what it is holding.
Here is an example…
I want to get value of ID from query string using this function.
var _newID= querySt(“ID”);
alert(_newID);
now I will get one alert with value of id…..