Search This Blog

Thursday, March 28, 2019

jQuery foreach: Using jQuery $.each

Step1:

Welcome to designers dairy, here we are going to discuss about jquery foreach and $.each
Learn how to loop through elements, arrays and objects with jQuery using the $.each() function, jQuery’s foreach equivalent.
jQuery’s foreach equivalent can be very useful for many situations. These examples will get you started and teach you how you can loop through arrays, objects and all kinds of HTML elements.

Step2:

Loop through an Array with jQuery foreach

With most languages you are probably going to need to loop through an array at some point, this is where foreach often becomes very useful. Below is some example jQuery that will loop through an array and output each entry to the console.

$(function () {
    var myArray = ["one", "two", "three", "four", "five"];
    $.each(myArray, function (index, value) {
        console.log(value);
    });
});

The output of the above will be:
one
two
three
four
five

Step3:

 Loop through Objects with jQuery foreach

As well as arrays, you will often want to loop through objects, or arrays of objects, especially when working with JSON data. The following code is an example of looping through some JSON objects.

$(function () {
var myObjects = [
{
id: 1,
firstname: "Gandalf",
lastname: "Greyhaem"
},
{
id: 2,
firstname: "Bilbo",
lastname: "Baggins"
},
{
id: 3,
firstname: "Frodo",
lastname: "Baggins"
}
];
$.each(myObjects, function () {
console.log("ID: " + this.id);
console.log("First Name: " + this.firstname);
console.log("Last Name: " + this.lastname);
console.log(" ");
});
})

The output of the above would be:
ID: 1
First Name: Gandalf
Last Name: Greyhaem
ID: 2
First Name: Bilbo
Last Name: baggins
ID: 3
First Name: Frodo
Last Name: Baggins

Step4:
Loop through a list with jQuery foreach

The ability to be able to loop through elements in a list can be very useful for all kinds of tasks. The code below will loop through every <li> element that has a parent with the .myList class.


$(function () {
$('.myList li').each(function () {
$(this).css("color", "green");
});
});

Step5:


Loop through other HTML elements with jQuery foreach
As-well as list elements there are benefits to being able to loop through all kinds of HTML elements. Below are a range of examples to demonstrate a few things that are possible.



$(function () {
//Loop through all links on the page
$('a').each(function(){
//Disable the link
$(this).prop("href", "#");
});
//Loop through all elements with the class .hide
$('.hide').each(function(){
//Hide it
$(this).hide(); //Alternative would be $(this).css("display", "none");
});
//Find all pre tags an add a class to them
$('pre').each(function(){
$(this).addClass("prettyprint");
});
});
So, there you have a load of examples that should get you started. There are many ways you can utilise the jQuery $.each method.

Wednesday, March 27, 2019

How to Check, if title attribute has content or not using jquery

Step1:

Welcome to designers dairy, here we are going to Check, if  the title attribute has content or not using
jquery


Step2:

Excepted Screen:







Step3:

Html Code:


<table class="table table-bordered">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td title="hello">John</td>
        <td title =" "></td>
         <td><a href="#" title="July"> july </a></td>
      </tr>
      <tr>
            <td title="hello">John</td>
        <td title="  "></td>
     <td><a href="#" title="July"> july </a></td>
      </tr>
       
         <tr>
        <td title="hello">Mary</td>
        <td title="  "></td>
         <td><a href="#" title="July"> july </a></td>
      </tr>
      <tr>
           <td title="hello">John</td>
       <td title="  "></td>
          <td><a href="#" title="July"> july </a></td>
     
      </tr>
            <tr>
           <td title="hello">John</td>
         <td title="  "></td>
        <td><a href="#" title="July"> july </a></td>
      </tr>
    </tbody>
  </table>


Step4:

There are the two methods to check the attribute title has content or not.

                                           1. Each Method() .prop() method provides a way to explicitly retrieve property values
                                          2. Filter Method()


Let us see in the script 


<script>

$(document).ready(function()
{

      Step1 :- Method using filter option: 

       $("td").filter(function(){
        
        var tittleStatus = ($(this).attr('title') || "hello").trim().length==1;
        
        return $(this).find('a').length <= 0 && tittleStatus;   

            }).css("background-color", "red");
            

        });

      Step2 :-  Method using each and prop method option:

        $("td").each(function(){

           if($(this).prop("title").trim().length == 0 && $(this).prop("title")!= "") {

             $(this).css("background-color", "blue")
         
             }
            
            else {
                
                 $(this).css("background-color", "red");
            }
                             
        });

      Step3 :-  Simple Method using filter method:

          $("td").filter(function()

             {
         return ($(this).attr('title') || '').trim().length == 0  &&  $(this).find('a').length <= 0;
                

            }).css("background-color", "red")
 });

</script>


Step5:


To check if <img> tag has title attribute set:
if($("img#someID").attr("title") !== undefined) {
    // ....
}
To select images that have title attribute:
$("img[title]")
Note:
  • Since jQuery 1.6 the attr() method returns undefined if the attribute was not present and ""if the attribute was present but empty
  • In e arlier versions of jQuery the attr() method returns "" if attribute was not present or it was present but empty

Thanks for reading my article.Stay Tune on www.webdesignersdairy.com  for more updates on Jquery.

Monday, March 18, 2019

How to scroll up the page using jquery

Welcome to  designer dairy blog, here we are going to  discuss how to scroll up the page using jquery.

Step1:

In my scenario, I have the fixed header and the content area consists of the anchor tag link. when the user clicks the link it should be moved to appropriate section.

Step2:



Script:


<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>

<script>

$(function(){

$('a[href^="#"]').not('[href=#]').click(function(e){

var anchorTag=$(this).attr('href').replace('.','\\.');

$('html, body').animate({scrollTop: $(anchorTag).offset().top - ($('.searchbar').outerHeight()+5) }, 1000);

return false

;

}

);

Step3:

Thanks for reading my article.Stay Tune on www.webdesignersdairy.com  for more updates on Jquery.

Validating to select in sequencial order using angular

    < input type = "checkbox" (change) = "handleSelectTaskItem($event, taskItem)" [checked] = " taskItem . i...