Showing posts with label pair. Show all posts
Showing posts with label pair. Show all posts

Thursday, 18 February 2016

How to Get TD Class Value in JqueryIn Feburary 2016 18,

In Feburary 2016 18,
Open the Web page in Notepad or a code editor, and look for the jQuery library file. If your code contains no reference to this file, add this code:Place this reference between the '
' tags of your Web page code or just above the closing '
' tag.
Add a pair of 'Write all jQuery code between the '
' tags.
Write a function to check for when the document finishes loading. This is also known as the 'document ready' function:$(function () {});The above code is short-hand for '$(document).ready()'.
Declare an array type variable that will hold the class values of your '' tags:var tdValue = [];Add this code between the curly braces of the 'document ready' function.
Iterate through each instance of '' in the Web page code by creating an 'each()' loop:$('td').each(function(i) {});This code goes after the array declaration. Note that 'each()' attaches to the 'td' selector, and a variable 'i' is passed as a parameter in the function. You need the 'i' variable to get values from the array.
Set your array equal to the results of the 'attr()' function inside the 'each()' loop:tdValue[i] = $(this).attr('class');The 'this' selector gets the parent selector, in this case 'td' from the selector in the line of code above it. Use 'class' inside 'attr()' to get the value of the class attribute for each '' tag.
Output the class name for each '' tag as text in each table cell by adding this line of code after the line containing your 'attr()' function:$(this).text(tdValue[i]);The finished script should look like this:$(function() {var tdValue = [];$('td').each(function(i) {
tdValue[i] = $(this).attr('class');$(this).text(tdValue[i]);
});});
In Feburary 2016 18,