jQuery Element Selector04:33

  • 0
Published on March 24, 2017

Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

In this video we will discuss jQuery Element Selector, i.e selecting elements by tag name.

To select the elements by tag name use jQuery Element Selector

Syntax : $(element)

$(‘td’) // Selects all td elements
$(‘div a’) // Select all anchor elements that are descendants of div element
$(‘div, span, a’) // Selects all div, span and anchor elements

Alerts the total count of td elements on the page
[script type=”text/javascript”]
$(document).ready(function () {
alert($(‘td’).length);
});
[/script]

Selects all the tr elements on the page and changes their background colour to red
[script type=”text/javascript”]
$(document).ready(function () {
$(‘tr’).css(‘background-Color’, ‘red’);
});
[/script]

Alerts the HTML content of the table
[script type=”text/javascript”]
$(document).ready(function () {
alert($(‘table’).html());
});
[/script]

Alerts the HTML content of each table row
[script type=”text/javascript”]
$(document).ready(function () {
$(‘table tr’).each(function () {
alert($(this).html());
});
});
[/script]

Select and changes the background colour of all the div, span and anchor elements
[script type=”text/javascript”]
$(document).ready(function () {
$(‘div, span, a’).css(‘background-Color’, ‘yellow’);
});
[/script]

Select all anchor elements that are descendants of div element
[html]
[head]
[title][/title]
[script src=”jquery-1.11.2.js”][/script]
[script type=”text/javascript”]
$(document).ready(function () {
$(‘div a’).css(‘background-Color’, ‘yellow’);
});
[/script]
[/head]
[body]
[div]
[a href=”
[/div]
[br /]
[a href=”
[/body]
[/html]

Changes the background color of even rows to gray and odd rows to yellow in both the tables.
[script type=”text/javascript”]
$(document).ready(function () {
$(‘tr:even’).css(‘background-Color’, ‘gray’);
$(‘tr:odd’).css(‘background-Color’, ‘yellow’);
});
[/script]

To change the background color of even rows to gray and odd rows to yellow just for one of the table, use #id selector along with element selector.

[script type=”text/javascript”]
$(document).ready(function () {
$(‘#table1 tr:even’).css(‘background-Color’, ‘gray’);
$(‘#table1 tr:odd’).css(‘background-Color’, ‘yellow’);
});
[/script]

Enjoyed this video?
"No Thanks. Please Close This Box!"