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


i using below code generate 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>");         (row = 1; row < 5; row++) {              document.write("<tr>");              (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 select button clicked table row should highlight entire row. idea how implement in javascript & css ?

here go! add function button onclick while creating , write function below:

demo

so changed button html before appending

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

and function

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

update

to remove previous selected on click of other below 1 of approach can opt to:

demo

css

.backchange{     background:red; } 

js

scenario 1

function highlight(ctrl){    var elements=document.getelementsbytagname('tr'); //get tr elements    for(var i=0;i<elements.length;i++)         elements[i].classname=''; //clear classname tr elements    var parent=ctrl.parentnode.parentnode;    parent.classname="backchange"; //add classname element's parent tr  } 

scenario 2

now if have classlist there in tr , want add or remove 1 particular class changes style can below:

demo

function highlight(ctrl){    var elements=document.getelementsbytagname('tr');     for(var i=0;i<elements.length;i++)         elements[i].classlist.remove('backchange'); //remove 1 particular class list of classnames in element    var parent=ctrl.parentnode.parentnode;    parent.classlist.add("backchange");//add particular class classlist of element's parent tr } 

update 2

without using class , applying inline styles can try 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 specific element.  } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -