Set table class javascript

jQuery Set class in HTML table, but not for the entire column (Demo 2)

The following tutorial shows you how to do «jQuery Set class in HTML table, but not for the entire column (Demo 2)».

The result is illustrated in the iframe.

You can check the full source code and open it in another tab using the links.

Javascript Source Code

The Javascript source code to do «jQuery Set class in HTML table, but not for the entire column (Demo 2)» is

$("td:nth-child(10):contains('1')").siblings('td:nth-child(7), td:nth-child(8)').addClass('disaster'); $("td:nth-child(10):contains('2')").siblings('td:nth-child(7), td:nth-child(8)').addClass('high'); $("td:nth-child(10):contains('3')").siblings('td:nth-child(7), td:nth-child(8)').addClass('average'); $("td:nth-child(10):contains('4')").siblings('td:nth-child(7), td:nth-child(8)').addClass('warning'); $("td:nth-child(10):contains('5')").siblings('td:nth-child(7), td:nth-child(8)').addClass('information'); //My code for XML import, not of need here. Just to show where the jQuery is located //XML Import & TBody Generation window.addEventListener("load", function() < getRows();// w w w . d e m o 2 s . c o m >); function getRows() < var xmlhttp = new XMLHttpRequest(); xmlhttp.open("get", "outage.xml", true); xmlhttp.onreadystatechange = function() < if (this.readyState == 4 && this.status == 200) < showResult(this); >>; xmlhttp.send(null); > function showResult(xmlhttp) < var xmlDoc = xmlhttp.responseXML.documentElement; removeWhitespace(xmlDoc); var outputResult = document.getElementById("TableData"); var rowData = xmlDoc.getElementsByTagName("tt-outageRow"); addTableRowsFromXmlDoc(rowData,outputResult); > function addTableRowsFromXmlDoc(xmlNodes,tableNode) < var theTable = tableNode.parentNode; var outage_start, check_status, client_name; for (i=0; ilength; i++) < newRow = tableNode.insertRow(i); outage_start = newRow.insertCell(newRow.cells.length); outage_start.innerHTML = xmlNodes[i].childNodes[4].firstChild.nodeValue; check_status = newRow.insertCell(newRow.cells.length); check_status.innerHTML = xmlNodes[i].childNodes[0].firstChild.nodeValue; check_status = newRow.insertCell(newRow.cells.length); check_status.innerHTML = xmlNodes[i].childNodes[1].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[7].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[2].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[3].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[5].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[6].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[8].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[9].firstChild.nodeValue; //THIS IS WHERE THE JQUERY IS LOCATED. > theTable.appendChild(tableNode); > function removeWhitespace(xml) < var loopIndex; for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++) < var currentNode = xml.childNodes[loopIndex]; if (currentNode.nodeType == 1) < removeWhitespace(currentNode); >if (!(/\S/.test(currentNode.nodeValue)) && (currentNode.nodeType == 3)) < xml.removeChild(xml.childNodes[loopIndex--]); >> >
html> head> title>jQuery problem meta name="viewport" content="width=device-width, initial-scale=1"> script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js" > style id="compiled-css" type="text/css"> /*CSS for color classes*/ td:nth-child(10) < opacity: 0; >!-- w w w . d e m o 2 s. c o m --> td.disaster < background-color: #E45858 > td.high < background-color: #E87658 > td.average < background-color: #FEA058 > td.warning < background-color: #FEC858 > td.information < background-color: #7498FE > /*CSS for main elements*/ div < max-width: 2600px; display: block; > body < font-family: Arial, Tahoma, Verdana, sans-serif; background-color: #FFFFFF; > table < text-align: left; border-collapse: collapse; > th < font-size: 75%; font-weight: normal; color: #768C98; padding-top: 5px; padding-bottom: 5px; border-bottom: 2px solid #DCE2E4; > td < font-size: 75%; color: #1F2C33; height: 25px; padding-top: 1px; padding-bottom: 1px; border-bottom: 1px solid #EAEEF0; > img < position: absolute; left: -100px; margin-top: 165px; transform: rotate(270deg); > /*CSS for Hover*/ td:nth-child(1):hover < text-decoration: underline; >td:nth-child(1) < background-color: #FFFFFF; > td:nth-child(2) < background-color: #FFFFFF; > tr.NoHover:hover< background-color: #FFFFFF; > tr:hover < background-color: #E8F5FF; > /*Column specific CSS*/ th.col1 < text-align: right; width: 240px; padding-right: 18px > th.col2 < width: 11px; padding: none; > th.col3 < text-align: left; width: 188px; padding-left: 10px; > th.col4 < text-align: left; width: 70px; > th.col5 < text-align: left; width: 77px; padding-left: 82px; > th.col6 < text-align: left; width: 430px; > th.col7 < text-align: left; padding-left: 10px; width: 497px; > th.col8 < text-align: left; width: 498px; > th.col9 < text-align: left; padding-left: 10px; width: 75px; > td:nth-child(1) < text-align: right; color: #0274B8; padding-right: 18px; border-right: 2px solid #AAD6F0; border-bottom: none; > td:nth-child(2) < color: white; border-bottom: none; width: 11px; padding: none; > td:nth-child(3) < text-align: left; text-decoration: underline dotted; padding-left: 10px; border-bottom: 1px solid #EAEEF0; > td:nth-child(4) < text-align: left; color: #DC0000; border-bottom: 1px solid #EAEEF0; > td:nth-child(5) < text-align: right; text-decoration: underline dotted; padding-right: 15px; border-bottom: 1px solid #EAEEF0; > td:nth-child(6) < text-align: left; text-decoration: underline dotted; border-bottom: 1px solid #EAEEF0; > td:nth-child(7) < text-align: left; text-decoration: underline dotted ; padding-left: 10px; border-bottom: 1px solid #EAEEF0; > td:nth-child(8) < text-align: left; text-decoration: underline dotted; border-bottom: 1px solid #EAEEF0; > td:nth-child(9) < text-align: left; padding-left: 10px; border-bottom: 1px solid #EAEEF0; >  body> body> div id="main"> table id="Table"> thead> tr >"NoHover"> th >"col1" scope='col' >Time▼ th >"col2" scope='col' > th >"col3" scope='col' >Client th >"col4" scope='col' >Status th >"col5" scope='col' >Site th >"col6" scope='col' >Host th >"col7" scope='col' >Problem • Cause th >"col8" scope='col' > th >"col9" scope='col' >Frequency th >"col10" scope='col'>    tbody id="TableData"> tr>td>2017-11-22 td>1 td>Client 1 td>FAILING td>Site 1 td>PC1 td>test1 td>Unable to open service td>24x7 td>1 tr>td>2017-11-22 td>1 td>Client 2 td>FAILING td>Site 2 td>PC2 td>test2 td>Unable to open service td>24x7 td>2 tr>td>2017-11-22 td>1 td>Client 3 td>FAILING td>Site 3 td>PC3 td>test3 td>Unable to open service td>24x7 td>3 tr>td>2017-11-22 td>1 td>Client 4 td>FAILING td>Site 4 td>PC4 td>test4 td>Unable to open service td>24x7 td>4 tr>td>2017-11-22 td>1 td>Client 5 td>FAILING td>Site 5 td>PC5 td>test5 td>Unable to open service td>24x7 td>5      script type='text/javascript'> $("td:nth-child(10):contains('1')").siblings('td:nth-child(7), td:nth-child(8)').addClass('disaster'); $("td:nth-child(10):contains('2')").siblings('td:nth-child(7), td:nth-child(8)').addClass('high'); $("td:nth-child(10):contains('3')").siblings('td:nth-child(7), td:nth-child(8)').addClass('average'); $("td:nth-child(10):contains('4')").siblings('td:nth-child(7), td:nth-child(8)').addClass('warning'); $("td:nth-child(10):contains('5')").siblings('td:nth-child(7), td:nth-child(8)').addClass('information'); //My code for XML import, not of need here. Just to show where the jQuery is located //XML Import & TBody Generation window.addEventListener("load", function() < getRows(); >); function getRows() < var xmlhttp = new XMLHttpRequest(); xmlhttp.open("get", "outage.xml", true); xmlhttp.onreadystatechange = function() < if (this.readyState == 4 && this.status == 200) < showResult(this); >>; xmlhttp.send(null); > function showResult(xmlhttp) < var xmlDoc = xmlhttp.responseXML.documentElement; removeWhitespace(xmlDoc); var outputResult = document.getElementById("TableData"); var rowData = xmlDoc.getElementsByTagName("tt-outageRow"); addTableRowsFromXmlDoc(rowData,outputResult); > function addTableRowsFromXmlDoc(xmlNodes,tableNode) < var theTable = tableNode.parentNode; var outage_start, check_status, client_name; for (i=0; ixmlNodes.length; i++) < newRow = tableNode.insertRow(i); outage_start = newRow.insertCell(newRow.cells.length); outage_start.innerHTML = xmlNodes[i].childNodes[4].firstChild.nodeValue; check_status = newRow.insertCell(newRow.cells.length); check_status.innerHTML = xmlNodes[i].childNodes[0].firstChild.nodeValue; check_status = newRow.insertCell(newRow.cells.length); check_status.innerHTML = xmlNodes[i].childNodes[1].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[7].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[2].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[3].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[5].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[6].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[8].firstChild.nodeValue; client_name = newRow.insertCell(newRow.cells.length); client_name.innerHTML = xmlNodes[i].childNodes[9].firstChild.nodeValue; //THIS IS WHERE THE JQUERY IS LOCATED. > theTable.appendChild(tableNode); > function removeWhitespace(xml) < var loopIndex; for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++) < var currentNode = xml.childNodes[loopIndex]; if (currentNode.nodeType == 1) < removeWhitespace(currentNode); >if (!(/\S/.test(currentNode.nodeValue)) && (currentNode.nodeType == 3)) < xml.removeChild(xml.childNodes[loopIndex--]); > > >   

  • jQuery Set all select elements in a table to the same width as the widest one in its column
  • jQuery set background colour for table when the page loads
  • jQuery Set class in HTML table, but not for the entire column
  • jQuery Set class in HTML table, but not for the entire column (Demo 2)
  • jQuery Set column width in table with CSS without using or changing the HTML
  • jQuery Set width of table cell relative to other cell in column
  • jQuery Set width of table cell relative to other cell in column (Demo 2)

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Add class and remove class using javascript for entire table

In this example I will show you how to add class and remove class using javascript. It can be achieved by using ClassName property. We can get all elements in the document with the specified tag name. Here I passed TR tag to the method getElementByTagName () and it will returns collection of all elements with specified tag element as object. With the help of object length by looping we can identify the class name and replace it. Example: try it yourself

html>
head>
script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">script>
script type="text/javascript">
$(document).ready(function ()
$("#btnChange").click(function ()
var tr = document.getElementsByTagName("tr");
for (var i = 0; i < tr.length; i++)
if (tr[i].className === 'highlight')
tr[i].className = 'normal';
>
>
>);
>);
script>
style type="text/css">
td
border-bottom:1px solid #808080;
>
.highlight
background-color: #2a78ac;
color:white;
>
.normal
background-color: #FFFCC9;
>

style>
head>
body style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">font-family: Arial; width: 500px;border:1px solid #e5dddd">
table>
tr class="highlight">
td>Tiger Nixontd>
td>System Architecttd>
td>Edinburghtd>
td>61td>
tr>
tr>
td>Garrett Winterstd>
td>Accountanttd>
td>Tokyotd>
td>63td>
tr>
tr class="highlight">
td>Ashton Coxtd>
td>Junior Technical Authortd>
td>San Franciscotd>
td>42td>
tr>
tr >
td>Cedric Kellytd>
td>Senior Javascript Developertd>
td>Edinburghtd>
td>22td>
tr>
table>
button id="btnChange" >changebutton>
body>
html>

Add class and remove class using javascript for entire table

Output: But in jQuery it is very easy with single line of code. It reduces the large amount of code.

$("tr.highlight").removeClass("highlight").addClass("normal");

Related Article

Источник

Читайте также:  Python execute code object
Оцените статью