简体   繁体   中英

Highlight row when clicked on button

I am using following script to highlight row when I clicked edit button of that row. I am passing Id of row when I click on button! My problem is the code is working in Mozila Firefox but not on Google Chrome. What is wrong in following code.

function high(id)
{
    $('tr').removeAttr('style'); 
    document.getElementById(id).style="background-color:#eeeeea;color:#000000;font-weight:500;";
}

Here is a demo to add special class to the editing row and remove the class on the other rows. This is done using the closest() method of jquery. You even do not need to use any id for this.

 $("button").on("click",function(){ $("tr").each(function(){ $(this).removeClass("marked"); }); $(this).closest("tr").addClass("marked"); }); 
  .marked{ background-color:#eeeeea;color:#000000;font-weight:500; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table> <tr> <td>This is TD</td> <td><button>Edit</button></td> </tr> <tr> <td>This is TD</td> <td><button>Edit</button></td> </tr> <tr> <td>This is TD</td> <td><button>Edit</button></td> </tr> </table> 

You need to set properties of style object individually.

var elem = document.getElementById(id);
//Set properties
elem.style.color = "#000000";
elem.style.fontWeight = "500";
elem.style.backgroundColor = "#eeeeea";

Since you are using , You can use .css()

$('#' + id).css({
    "background-color" :"#eeeeea",
    "color":"#000000",
    "font-weight":"500";
});

However, I would recommend you to create a CSS class then use it.

$('tr').removeClass('highlight'); 
$('#' + id).addClass('highlight');

Try this,

$('#'+id).attr("style","background-color:#eeeeea;color:#000000;font-weight:500;");

Working on chrome also.

The reason, can be style is an object which has some properties in it and chrome may not allow to overwrite it. So the custom string you passed in style will not apply to it, hence no changes applied to the element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM