Skip to content Skip to sidebar Skip to footer

Html Table With Button On Each Row

I have a table with multiple rows and one column. Each table cell has a button in it. Like this: &

Solution 1:

Pretty sure this solves what you're looking for:

HTML:

<table><tr><td><buttonclass="editbtn">edit</button></td></tr><tr><td><buttonclass="editbtn">edit</button></td></tr><tr><td><buttonclass="editbtn">edit</button></td></tr><tr><td><buttonclass="editbtn">edit</button></td></tr></table>

Javascript (using jQuery):

$(document).ready(function(){
    $('.editbtn').click(function(){
        $(this).html($(this).html() == 'edit' ? 'modify' : 'edit');
    });
});

Edit:

Apparently I should have looked at your sample code first ;)

You need to change (at least) the ID attribute of each element. The ID is the unique identifier for each element on the page, meaning that if you have multiple items with the same ID, you'll get conflicts.

By using classes, you can apply the same logic to multiple elements without any conflicts.

JSFiddle sample

Solution 2:

Put a single listener on the table. When it gets a click from an input with a button that has a name of "edit" and value "edit", change its value to "modify". Get rid of the input's id (they aren't used for anything here), or make them all unique.

<scripttype="text/javascript">functionhandleClick(evt) {
  var node = evt.target || evt.srcElement;
  if (node.name == 'edit') {
    node.value = "Modify";
  }
}

</script><tableid="table1"border="1"onclick="handleClick(event);"><thead><tr><th>Select
    </thead><tbody><tr><td><formname="f1"action="#" ><inputid="edit1"type="submit"name="edit"value="Edit"></form><tr><td><formname="f2"action="#" ><inputid="edit2"type="submit"name="edit"value="Edit"></form><tr><td><formname="f3"action="#" ><inputid="edit3"type="submit"name="edit"value="Edit"></form></tbody></table>

Post a Comment for "Html Table With Button On Each Row"