HTML+JavaScript: How to highlight a row on click of a button in Javascript?

Anurag Chakraborty

I am using below code to generate a dynamic table-

<!DOCTYPE html>
<html>
<head>
    <script>
        document.write("<table id=appTable border=1 style=margin-top:10px; margin-left:10px;>");
        document.write("<tr><th>Select</th><th>Name</th><th>Location</th><th>Action</th></tr>");
        for (row = 1; row < 5; row++) {

            document.write("<tr>");

            for (col = 1; col <= 4; col++) {
                if(col == 1) {
                    document.write("<td><input type='checkbox' id='mapCheck' name='myTextEditBox' /></td>");
                }
                if(col == 2) {
                    document.write("<td width='140'>Name</td>");
                }
                if(col == 3) {
                    document.write("<td width='200'>Location</td>");
                }
                if(col == 4) {
                    document.write("<td><button type='button'>select</button></td>");
                }
            }

            document.write("</tr>")

        }

        document.write("</table>")
    </script>

</body>
</html>

When the select button is clicked the table row should highlight the entire row. Any idea how to implement it in javascript & css ?

Guruprasad J Rao

Here you go! Add a function to your button onclick while creating it and write a function as below:

DEMO

So changed button html before appending will be

document.write("<td><button type='button' onclick='highlight(this)'>select</button></td>")
                                          ^^^^^Add this

and a function

function highlight(ctrl){
   var parent=ctrl.parentNode.parentNode;
   parent.style.background='red';
}

UPDATE

To remove previous selected on click of other below is one of the approach you can opt to:

DEMO

CSS

.backChange{
    background:red;
}

JS

Scenario 1

function highlight(ctrl){
   var elements=document.getElementsByTagName('tr'); //get all the tr elements
   for(var i=0;i<elements.length;i++)
        elements[i].className=''; //clear the className from all the tr elements
   var parent=ctrl.parentNode.parentNode;
   parent.className="backChange"; //add ClassName to only this element's parent tr

}

Scenario 2

Now If you have classList that are already there in tr and you just want to add or remove one particular class which changes its style then you can do it as below:

DEMO

function highlight(ctrl){
   var elements=document.getElementsByTagName('tr');
    for(var i=0;i<elements.length;i++)
        elements[i].classList.remove('backChange'); //remove one particular class from list of classNames in that element
   var parent=ctrl.parentNode.parentNode;
   parent.classList.add("backChange");//Add that particular class to classList of element's parent tr
}

UPDATE 2

Without using Class and applying inline styles you can try as below:

DEMO

function highlight(ctrl){
   var elements=document.getElementsByTagName('tr');
    for(var i=0;i<elements.length;i++)
        elements[i].style.background=''; //remove background color
   var parent=ctrl.parentNode.parentNode;
   parent.style.background="red";//add it to specific element.

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to call JavaScript function in html on button click

From Dev

Javascript/HTML button click error

From Dev

HTML/Javascript Button Click Counter

From Dev

How to call javascript function without button click?

From Dev

html/javascript how to display user input with a method on button click?

From Dev

Click a html button using javascript

From Dev

HTML,javascript: on button click validation not working

From Dev

Click button with javascript

From Dev

Simulate button click with JavaScript

From Dev

How to change css classes with a button click in javascript?

From Dev

Javascript clearInterval with button click

From Dev

Adding html content from javascript, and changing it in html on button click

From Dev

Selenium: how to click on javascript button

From Dev

Javascript - change html tag on button click

From Dev

How to activate text input on a button click in javascript

From Dev

How to differentiate between button click and button click through javascript

From Dev

javascript in html click button

From Dev

How can I pass value(contents) of HTML textbox to a javascript function on a button click?

From Dev

HTML/Javascript Button Click Counter

From Dev

Javascript/jquery Get Row Id on button click in a table

From Dev

How to call javascript function after click of a button?

From Dev

how to get javascript function on click on button

From Dev

How to change css classes with a button click in javascript?

From Dev

Click a button on the row of the table and remove the row using javascript

From Dev

Adding html content from javascript, and changing it in html on button click

From Dev

How to hide SVG elements with JavaScript on a button click?

From Dev

Click button in webpage with Javascript

From Dev

javascript - find and click a button

From Dev

Html button click and nodejs in javascript

Related Related

  1. 1

    Unable to call JavaScript function in html on button click

  2. 2

    Javascript/HTML button click error

  3. 3

    HTML/Javascript Button Click Counter

  4. 4

    How to call javascript function without button click?

  5. 5

    html/javascript how to display user input with a method on button click?

  6. 6

    Click a html button using javascript

  7. 7

    HTML,javascript: on button click validation not working

  8. 8

    Click button with javascript

  9. 9

    Simulate button click with JavaScript

  10. 10

    How to change css classes with a button click in javascript?

  11. 11

    Javascript clearInterval with button click

  12. 12

    Adding html content from javascript, and changing it in html on button click

  13. 13

    Selenium: how to click on javascript button

  14. 14

    Javascript - change html tag on button click

  15. 15

    How to activate text input on a button click in javascript

  16. 16

    How to differentiate between button click and button click through javascript

  17. 17

    javascript in html click button

  18. 18

    How can I pass value(contents) of HTML textbox to a javascript function on a button click?

  19. 19

    HTML/Javascript Button Click Counter

  20. 20

    Javascript/jquery Get Row Id on button click in a table

  21. 21

    How to call javascript function after click of a button?

  22. 22

    how to get javascript function on click on button

  23. 23

    How to change css classes with a button click in javascript?

  24. 24

    Click a button on the row of the table and remove the row using javascript

  25. 25

    Adding html content from javascript, and changing it in html on button click

  26. 26

    How to hide SVG elements with JavaScript on a button click?

  27. 27

    Click button in webpage with Javascript

  28. 28

    javascript - find and click a button

  29. 29

    Html button click and nodejs in javascript

HotTag

Archive