Showing posts with label Jquery. Show all posts
Showing posts with label Jquery. Show all posts

Friday, 16 October 2015

How to Get Selected CheckBox Values

This Article will Teach you How you can get Selected Checkbox Values with Comma Seperated in a Variable using Jquery.

Create a new Html File Named test.html and add the below code in that file.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<body>

    <input type="checkbox" class="form" value="E001" />
    <input type="checkbox" class="form" value="E002" />
    <input type="checkbox" class="form" value="E003" />
    <input type="checkbox" class="form" value="E004" />

    <input name="searchDonor" type="button" class="button" value="Get Value" onclick="getvalue()" />
    <script>
        function getvalue() {

            var boxesValue = [];
            $('input:checkbox.form:checked').each(function () {
                var value = (this.checked ? $(this).val() : "");
                boxesValue.push(value);
            });

            var str = boxesValue.join(",");
            alert(str);
        }
    </script>

</body>
</html>

Now Run This html file.
Click the checkboxes and Click on Get Value Button.
You will get Selected Check box Value.

Continue Reading →

Wednesday, 15 April 2015

Difference between bind(),on(), delegate() in Jquery

bind() method: This method only attaches events to elements which exist beforehand i.e. state of initialized document before the events are attached. If the selector condition is satisfied for an event afterward, bind() will not work on that function. It also won’t work in the case if selector condition is removed from the element.

<script>
    $("#foo").bind("mouseenter mouseleave"function () {
        $(this).toggleClass("entered");
    });

    $("#foo2").bind({
        click: function () {
            // Do something on click
        },
        mouseenter: function () {
            // Do something on mouseenter
        }
    });
    $("#foo").bind("click"function () {
        alert($(this).text());
    });
</script>

on() method: This method attaches events not only to existing elements but also for the ones appended in the future as well. The difference here between on() and live() function is that on() method is still supported and uses a different syntax pattern, unlike the above two methods.


$"body" ).on"click""p"function() {
    alert$this ).text() );
  });

//Cancel a link's default action using the .preventDefault() method.
  $"body" ).on"click""a"functionevent ) {
    event.preventDefault();
  });

.delegate()
Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.
Syntax: .delegate( selector, eventType, handler )

For example, the following .delegate() code:

$"table" ).delegate"td""click"function() {
    $this ).toggleClass"chosen" );
  });

is equivalent to the following code written using .on():

$"table" ).on"click""td"function() {
    $this ).toggleClass"chosen" );
  });

The .delegate() method is very powerful, The difference between .live() and .delegate() is, live function can't be used in chaining. live function needs to be used directly on a selector/element. Also .delegate() works on dynamically added elements to the DOM where the selectors match. Chaining is supported correctly in .delegate().

Continue Reading →

Friday, 9 January 2015

Return Selected Value from listbox in Comma seperated

<html>
<head>
    <script src="jquery.min.js"></script>
    <script>
        $("document").ready(function () 
         {
                var val = [];
                $("#myselect option:selected").each(function () {
                    val.push(this.text);
                });
                alert(val.join(','));  //Output: Mr,Ms,Prof
        });
    </script>
</head>
<body>
    <select id="myselect" multiple="multiple">
        <option value="1" selected="selected">Mr</option>
        <option value="2">Mrs</option>
        <option value="3" selected="selected">Ms</option>
        <option value="4">Dr</option>
        <option value="5" selected="selected">Prof</option>
    </select>

</body>

</html>

Download this Clickhere
Continue Reading →

Friday, 31 October 2014

Javascript Vs Jquery

JavaScript: A powerful language in web development
JavaScript is a scripting language that is used to add interactivity to our web pages. It is one of the three core technologies alongside HTML and CSS which are used to create web pages. 
JavaScript is supported by all the web browsers and the web browsers have a built-in JavaScript engine to identify JavaScript code and work with it. Thus, JavaScript is majorly a client-side language.

jQuery: A library developed from JavaScript
jQuery is a library of JavaScript which is built from it. It is the most popular JavaScript library. jQuery is free, an open-source library, licensed under the MIT License. This has a powerful feature of cross-browser compatibility. It can easily handle cross-browser issues that we can face with JavaScript. Thus many developers use jQuery to avoid cross-browser compatibility issues.

Why jQuery is created and what are the special capabilities of jQuery?
In JavaScript, we have to write a lot of code for basic operations while with jQuery the same operations can be done with a single line of code. Therefore developers find it easier to work with jQuery than with JavaScript.

DOM Traversal and Manipulation

In JavaScript:

We can select a DOM element in JavaScript using the document.getElementById() method or by using the document.querySelector() method.

var mydiv = document.querySelector(“#div1”);
//Or
document.getElementById(“#div1”);

In jQuery:

Here, we will have to only use the $ symbol with the selector in brackets.

$(selector)

$("#div1") – The selector is an id ‘div1
$(".div1") – The selector is a class ‘div1
$("#P") – The selector is the paragraph in the Html page

Adding styles in JavaScript:
document.getElementById ('myDiv').style.backgroundColor="#FFF"

Adding styles in jQuery:
$('#myDiv').css (‘background-color','#FFF');

Continue Reading →

Monday, 14 April 2014

7 jQuery Code Snippets every web developer must have

jQuery extensively simplified web developer's life and has become a leader in javascript available libraries. There are a lot of useful jQuery snippets available but here in this post I am going to share 7 basic and widely used code snippets that every front-end web developer must have. Even for those who are new to jQuery can easily understand and get benefit from these routinely used code snippets.

1. Print Page Option
Providing option to print a page is a common task for web developers. Following is the available code:

<!-- jQuery: Print Page -->

$('a.printPage').click(function(){

           window.print();

           return false;
}); 

<!-- HTML: Print Page -->

<div>
<a  class="printPage" href="#">Print</a>
</div>


2. Helping Input Field/Swap Input Field

In order to make an Input Text field helpful, we normally display some default text inside it (For Example "Company Name") and when user click on it, text disappears and user can enter the value for it.
You can try it yourself by using the following code snippet.

<!-- jQuery: Helping Input Field -->
$('input[type=text]').focus(function(){    
           var $this = $(this);
           var title = $this.attr('title');
           if($this.val() == title)
           {
               $this.val('');
           }
}).blur(function() {
           var $this = $(this);
           var title = $this.attr('title');
           if($this.val() == '')
           {
               $this.val(title);
           }
});

<!-- HTML: Swap Input Field -->

<div>
       <input type="text" 
name="searchCompanyName"
value="Company Name" 
title="Company Name" />
</div>


3. Select/Deselect All options

Selecting or Deselecting all available checkbox options using a link on HTML page is common task.

<!-- jQuery: Select/Deselect All -->
$('.SelectAll').live('click', function(){ $(this).closest('.divAll').find('input[type=checkbox]').attr('checked', true); return false; }); $('.DeselectAll').live('click', function(){ $(this).closest('.divAll').find('input[type=checkbox]').attr('checked', false); return false; });

<!-- HTML: Select/Deselect All -->

<div class="divAll"> <a href="#" class="SelectAll">Select All</a>&nbsp; <a href="#" class="DeselectAll">Deselect All</a> <br /> <input type="checkbox" id="Lahore" /><label for="Lahore">Lahore</label> <input type="checkbox" id="Karachi" /><label for="Karachi">Karachi</label> <input type="checkbox" id="Islamabad" /><label for="Islamabad">Islamabad</label> </div>


4. Disabling Right Click

For web developers, its common to disable right click on certain pages so following code will do the job.

<!-- jQuery: Disabling Right Click -->
$(document).bind("contextmenu",function(e){
       e.preventDefault();


   });


5. Identify which key is pressed.
Sometimes, we need to validate the input value on a textbox. For example, for "First Name" we might need to avoid numeric values. So, we need to identify which key is pressed and then perform the action accordingly.
<!-- jQuery: Which key is Pressed. -->
$('#txtFirstName').keypress(function(event){
     alert(event.keyCode);
  });

<!-- HTML: Which key is Pressed. -->
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>


6. Validating an email.
Validating an email address is very common task on HTML form.

<!-- jQuery: Validating an email. -->
$('#txtEmail').blur(function(e) {
            var sEmail = $('#txtEmail').val();
            if ($.trim(sEmail).length == 0) {
                alert('Please enter valid email address');
                e.preventDefault();
            }        
            var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]
                             {2,4}|[0-9]{1,3})(\]?)$/;        
            if (filter.test(sEmail)) {
                alert('Valid Email');
            }
            else {
                alert('Invalid Email');
                e.preventDefault();
            }
        });

<!-- HTML: Validating an email-->
<asp:TextBox id="txtEmail" runat="server" />


7. Limiting MaxLength for TextArea
Lastly, it usual to put a textarea on a form and validate maximum number of characters on it.

<!-- jQuery: Limiting MaLength for TextArea -->
   var MaxLength = 500;
       $('#txtDescription').keypress(function(e)
       {
          if ($(this).val().length >= MaxLength) {
          e.preventDefault();}
       });

<!-- HTML: Limiting MaLength for TextArea-->
<asp:TextBox ID="txtDescription" runat="server" 
                         TextMode="MultiLine" Columns="50" Rows="5"></asp:TextBox>


This is my selection of jQuery code snippets but jQuery is a very powerful client-side framework and a lot more can be done using it.
Continue Reading →

Wednesday, 9 April 2014

Logout Application while Click on Back or Farward Button in Browser


// -- this Script is for Logout Application while Clicking on Back or Farward button in Browser

        jQuery(document).ready(function ($) {
            if (window.history && window.history.pushState) {
                window.history.pushState('forward', null, './');
                $(window).on('popstate', function () {
                    window.location.href = "/Account/LogOff";
                });
            }
        });
Continue Reading →

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (55) CD (1) CI (2) CloudComputing (2) Coding (10) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (20) Entity Framework (5) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) jwtToken (4) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives