Recent Posts

Responsive Ads Here
Showing posts with label Exceptions. Show all posts
Showing posts with label Exceptions. Show all posts

Tuesday, 22 March 2016

Count days between two days through javascript

Count days between two days through javascript

In real time applications this scenario is very important for counting days between two selected days.Here I have taken Start Date and End Date for counting days between start date and end date.For that I have taken two input fields as type date with ids as “startDate” ,”endDate”.

General Scenario: Suppose in your application, you are going to create new program for that you must select start and end date and also print how many days between two selected days by automatically.

Why we give id to the input field?
Because find and read appropriate input values.Here I have taken startDate and endDate as ids to the Start Date  and End Date fields. Because in script I can read entered values in input field through this ids only.So ids are must be unique.

<!DOCTYPE html>
<html>
<body>
<h1>Count between two days through javascript</h1>
<h4>Select Start Date</h4>
<input type="date" id="startDate" onChange="onDateChange()"/>
<h4>Select End date </h4>
<input type="date" id="endDate" onChange="onDateChange()"/>
<p>Difference is :<p id="demo"> </p></p>
<script>
function onDateChange()
{
var sDate=new Date(document.getElementById("startDate").value);
var eDate=new Date(document.getElementById("endDate").value);
var timeDiff=Math.abs(eDate.getTime()-sDate.getTime());
var diffDays=Math.ceil(timeDiff/(1000*3600*24));
document.getElementById("demo").innerHTML = diffDays;
}
</script>
</body>
</html>

Process:

   1.Whenever  we select dates ,action goes to script with onDateChange() function
   2.Read startdate and endDate values
   3.Find time difference between two selected days
   4.Find days difference between two selected days 
  5.For print out put on page ,I have taken <p id="demo">  ,in script assign values I used document.getElementById("demo").innerHTML = diffDays;

Note : You can do same thing in jquery  also count days between two selected dates.
You  can watch videotutorial on this example. 

count days between two dates through Jquery

count days between two dates through Jquery


In real time applications this scenario is very important for counting days between two selected dates.
Here I have taken Start Date and End Date for counting days between start date and end date.For that I have taken two input fields as type date with ids as “startDate” ,”endDate”.

General Scenario : Suppose in your application, you are going to create new program for that you must select start and end date and also print how many days between two selected dates by automatically.
Why we give id to the input field?
Because find and read appropriate input values.Here I have taken startDate and endDate as ids to the Start Date  and End Date fields.Becacuse in script I can read entered values in input field through this ids only.So ids are must be unique.

<!DOCTYPE html>
<html>
<body>
<h1>Count between two days through JQuery</h1>
<h4>Select Start Date</h4>
<input type="date" class="date"  id="startDate"/>
<h4>Select End date </h4>
<input type="date" class="date"  id="endDate"/>
<p>Difference is :<p id="demo"> </p></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(function() {
$('.date').change(function() {
var sDate=new Date($("#startDate").val());
var eDate=new Date($("#endDate").val());
var timeDiff=Math.abs(eDate.getTime()-sDate.getTime());
var diffDays=Math.ceil(timeDiff/(1000*3600*24));
document.getElementById("demo").innerHTML = diffDays;
 });
});
</script>
</body>
</html>

Process :
      1.Whenever  we select dates ,action goes to script with change function
      2.  Read startdate and endDate values
      3.  Find time difference between two selected dates
      4. Find days difference between two selected dates
    5. For print out put on page ,I have taken <p id="demo"> ,in script assign values I used document.getElementById("demo").innerHTML = diffDays;

Note : You can do same thing in  javascript also count days between two selected dates
You  can watch video tutorial on this example.



Saturday, 19 December 2015

Bulb on off javascript example flow explanation



Source code For Example :
<!DOCTYPE html>
<html>
<body>
<img id="myImage" onclick="changeImage()" src="pic_bulboff.gif" width="100" height="180">
<p id="demo"></p>
<p id="position"></p>
<p id="conditionCheck"></p>

<script>
function changeImage() 
    {
    var image = document.getElementById('myImage');       
    if (image.src.match("bulbon")) 
     {      document.getElementById("conditionCheck").innerHTML=Boolean(image.src.match("bulbon"));
     image.src = "pic_bulboff.gif"; 
     text="i am in offfffffffff";
     document.getElementById("position").innerHTML=text;
    } 
    else 
     {
 document.getElementById("conditionCheck").innerHTML=Boolean(image.src.m  atch("bulbon"));
        image.src = "pic_bulbon.gif";
        text="i am in on";
       document.getElementById("position").innerHTML=text;
      }
       var k=document.getElementById('myImage').src;
       document.getElementById("demo").innerHTML=k; 
}
</script>

</body>
</html>

Courtesy : w3schools.com



Java Script flow steps explanation:-


1. By default  Intitally  control  goes to else condition because (<img id="myImage" onclick="changeImage()" src="pic_bulboff.gif" width="100" height="180">) in this image src contains bulboff url,so if condition fails ,directly control goes to else part ,in that else part we assign image.src="onbulb url"; so in image.src currently  having bulb on image url, so initially bulb is in off mode.,idea behind that is what action to be taken when user clicks on ...

2.Then next when you click on bulb image it will on ,because if condition executes .Initially we assign image src="bulbon".So if condition ,in if condition ,we assign again image src="bulboff" ,idea behind that is what action to be taken when user clicks off ...

3. It will repeat until you stop to click...



validate check boxes using java script and jquery



Here you can know about how to validate check boxes using jquery.When we select check box ,then it enable text fields with appropriate values ,if  we make it as un select  ,then it must be disable all text fields ....


<!DOCTYPE html>
<html>
<body>
<form id="formid"> 
<input type="text" id="id1" />
<input type="text" id="id2"  />
<input type="text" id="id3"  />
<input type="checkbox"  id="check" onclick="clickFun()">
<button type="submit" id="but">Submit</button></form>


<script>
$(document).ready(function()
{      
       $("#id1").hide();
        $("#id2").hide();
        $("#id3").hide();
$("#check").click(function()
{
     if($("#check").prop("checked"))
     {
        $("#id1").show();
        $("#id2").show();
        $("#id3").show();
        $("#id1").attr("required","required");
        $("#id2").attr("required","required");
        $("#id3").attr("required","required");
      }
    else
     {
        $("#id1").hide();
        $("#id2").hide();
        $("#id3").hide();
         $("#id1").removeAttr('required');
  $("#id2").removeAttr('required');
  $("#id3").removeAttr('required');
      }
    });
   });
</script>
</body>
</html>