How to change color of selected row on onmousedown event

html table row selected background color change
change table row background color onclick javascript
how to change selected html table row background color using javascript
javascript change table row background color
html table highlight selected row
jquery change table row background color onclick
change row color based on cell value javascript
javascript change table row background color based on value

I am trying to change the color of the selected row from a table on a onmousedown event and reset all others (or keep them the same) . Only one row can be red at a time while all others are green.

What I have tried:

function HighLight(id) {
  var rows = $('#tbl > tbody > tr').each(function(elem) {
    elem.style.background = 'green';
  })
  var tr = document.getElementById(id);
  tr.style.background = 'red';
}
<table id="tbl">
  <tr id="tr1" style="background-color:aquamarine" onmousedown="Highlight(e)">
    <td>
      v1
    </td>
  </tr>
  <tr id="tr2" style="background-color:aquamarine" onmousedown="Highlight(e)">
    <td>
      v2
    </td>
  </tr>
  <tr id="tr3" style="background-color:aquamarine" onmousedown="Highlight(e)">
    <td>
      v3
    </td>
  </tr>
</table>

The function name is wrong its Highlight not HighLight

To pass the id of the element on function call you cannot just pass any variable(e in your case). Use this.getAttribute('id') to get the id.

In the each() the argument elem represented the index of the element and not the element itself. Introduce another argument for index.

function Highlight(id) {
  var rows = $('#tbl > tbody > tr').each(function(i,elem) {
    elem.style.background = 'green';
  })
  var tr = document.getElementById(id);
  tr.style.background = 'red';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tbl">
  <tr id="tr1" style="background-color:aquamarine" onmousedown="Highlight(this.getAttribute('id'))">
    <td>
      v1
    </td>
  </tr>
  <tr id="tr2" style="background-color:aquamarine" onmousedown="Highlight(this.getAttribute('id'))">
    <td>
      v2
    </td>
  </tr>
  <tr id="tr3" style="background-color:aquamarine" onmousedown="Highlight(this.getAttribute('id'))">
    <td>
      v3
    </td>
  </tr>
</table>

onmousedown Event, The onmousedown event occurs when a user presses a mouse button over an the mouse button is pressed down over a <p> element, change its color to red. Definition and Usage. The onmousedown event occurs when a user presses a mouse button over an element. Tip: The order of events related to the onmousedown event (for the left/middle mouse button):

Here is a quick example on how can you do that.

$("table tr").on('click', function(){
  $(".highlighted").removeClass("highlighted");
  $(this).addClass("highlighted");
});
table tr {
  background: green;
}
table tr.highlighted {
  background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tbl">
  <tr id="tr1">
    <td>
      v1
    </td>
  </tr>
  <tr id="tr2">
    <td>
      v2
    </td>
  </tr>
  <tr id="tr3">
    <td>
      v3
    </td>
  </tr>
</table>

How To Change Selected HTML Table Row Background Color , Changing the color of selected row on click in javascript Source Code:�Duration: 13:56 Posted: Mar 30, 2017 How to change color of selected row on onmousedown event I am trying to change the color of the selected row from a table on a onmousedown event and reset all others (or keep them the same) . Only one row can be red at a time while all others are green.

You can have two css classes; one for selected row and other for remaining rows. On click of the row, you can add the "selected" class to that row.

$("#tbl tr").click(function(){
 
 var $this = $(this);
 //remove the previous row selection, if any
 $("#tbl tr.selected").removeClass("selected");
 //add selected class to the current row
 $this.addClass("selected");

});
#tbl tr{
background-color: aquamarine;
}

#tbl tr.selected{
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="tbl">
  <tr id="tr1">
    <td>
      v1
    </td>
  </tr>
  <tr id="tr2" >
    <td>
      v2
    </td>
  </tr>
  <tr id="tr3" >
    <td>
      v3
    </td>
  </tr>
</table>

How to highlight a table row on click using jQuery, //attach an onclick event handler to the table rows $(“#myTable tbody tr”).click( function(){ //change the 'background-color' property to the� The change event is fired for input, select, and textarea elements when an alteration to the element's value is committed by the user. Unlike the input event, the change event is not necessarily fired for each alteration to an element's value.

You can do like this.by using class you can carry out other operations

$("#tbl").on("click", "tr", function() {
  $(' tr').removeClass("Red")

  $(this).addClass("Red")
});
.Red {
  background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tbl">
  <tr id="tr1">
    <td>
      v1
    </td>
  </tr>
  <tr id="tr2">
    <td>
      v2
    </td>
  </tr>
  <tr id="tr3">
    <td>
      v3
    </td>
  </tr>
</table>

How to change an element color based on value of the color picker , To change the color of our element based on the value of color picker we have to use onclick() event of the element and change its CSS color property as per the� on DBGrid OnCellClick event handler put this: MessageDlg(IntToStr(Table1.Recno),mt Informatio n,[mbOK],0); Table1 is the dataset refered by the dbgrid each time you click the dbgrid then there will be window pop up shows the number of record selected.. (I tried it successfully using paradox). regards.

How to change the background color after clicking the button in , The on() method is used as event handlers for the selected elements and child elements. The css() method is used to change/set the background color of the� Use an OnMouseDown event handler - passes X & Y in stringgrid coordinates . 2 How do I find which cell was clicked on Use TStringGrid.MouseToCell . 3 How do I make the popup popup Use TPopUpMenu.PopUp - but it needs mouse X,Y in Screen coordinates, So use TStringGrid.ClientToScreen . Example is procedure TForm1.StringGrid1MouseDown(Sender

Is it possible to give a color to a row onClick � React Table, I have a click event on a cell in react -table and onClick of that cell i className or background style of the row based on your selected row. Note. The following events are not raised for the TabControl class unless there is at least one TabPage in the TabControl.TabPages collection: Click, DoubleClick, MouseDown, MouseUp, MouseHover, MouseEnter, MouseLeave and MouseMove.

change color of selected row � Issue #373 � react-bootstrap-table , How can I change the style of a selected row ? # @ change color of selected row #373 rowEvents={{ onClick: (e, row, rowIndex) => this.props. The example display all selected row, I want display only row when I click,� A GridViewRow object is created for each row in the control before the GridView is rendered. Whenever a row in the GridView gets created, the RowCreated event is fired. Using this event, we can customize the behavior of the GridView. For e.g.: adding client script to the row or customizing the content of the row.

How to change background color of row onclick?, to fit your purposes. There is a drop-down menu version for body background color at Table row "glow" is highlighted whatever color you select. getElementById && !ie function rowclick(e) { var obj = ns ? e.target : event. Value Description; color: Specifies the color of the text. Look at CSS Color Values for a complete list of possible color values: initial: Sets this property to its default value.

Comments
  • And when you say this how does it know to refer to a tr or a td ?
  • @BercoviciAdrian - depends on the click handler -> here the click is registered for tr -> $("#tbl tr").click