Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Friday, 4 August 2017

Delete HTML Table Row Using Javascript/JQuery Without RowIndex

Using JavaScript:

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<p>Click the button to delete the any row from the table.</p>

<table id="table1">
  <tr id="myRow1">
    <td>First cell</td>
    <td>Second cell</td>
    <td>Third cell</td>
    <td><input type="button" onclick="deleteRow('myRow1')" value="Delete"/></td>
  </tr>
  <tr id="myRow2">
    <td>First cell</td>
    <td>Second cell</td>
    <td>Third cell</td>
    <td><input type="button" onclick="deleteRow('myRow2')" value="Delete"/></td>
  </tr>
  <tr id="myRow3">
    <td>First cell</td>
    <td>Second cell</td>
    <td>Third cell</td>
    <td><input type="button" onclick="deleteRow('myRow3')" value="Delete"/></td>
  </tr>
</table><br>

<script>
function deleteRow(myRow) {
    var row = document.getElementById(myRow);
    var total_cell=document.getElementById('table1').rows[0].cells.length;

   for(i=0;i<total_cell;i++)
   {
    row.deleteCell(0);
    }
  
}

</script>

</body>
</html>


Using JQuery:


<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style>
table, td {
    border: 1px solid black;
}
</style>
</head>
<body>
<table id="table1">
  <tr id="myRow1">
    <td>First cell</td>
    <td>Second cell</td>
    <td>Third cell</td>
    <td><input type="button" onclick="deleteRow(this)" value="Delete"/></td>
  </tr>
  <tr id="myRow2">
    <td>First cell</td>
    <td>Second cell</td>
    <td>Third cell</td>
    <td><input type="button" onclick="deleteRow(this)" value="Delete"/></td>
  </tr>
  <tr id="myRow3">
    <td>First cell</td>
    <td>Second cell</td>
    <td>Third cell</td>
    <td><input type="button" onclick="deleteRow(this)" value="Delete"/></td>
  </tr>
</table><br>



<script>
function deleteRow(this_row) {
    $(this_row).closest("tr").html("");
  
}
</script>

</body>
</html>

Thursday, 22 September 2016

How to bind Dropdown Test and Value Dynamically and Retrive also?


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
function DynamicBind()
{
var Ttext="India";//CountryName -> this is visible
var Tvalue="CId-1";//CountryId -> this is invisible for secure back hand code

$("#textandvalueSET").append($('<option></option>').val("").html("-Select-"));
$("#textandvalueSET").append($('<option></option>').val(Tvalue).html(Ttext));

}

function ShowDropTextValue()
{
var countrySelect = document.getElementById("textandvalueSET");
var selectedText = countrySelect.options[countrySelect.selectedIndex].text;
alert(" Selected Text: "+selectedText);

var vv=document.getElementById("textandvalueSET").value;
alert(" Selected Value= "+vv);

}
</script>
</head>
<body>

<br/>
<br/>
<input type="button" value="Dynamic" onclick="DynamicBind()"/>
<select id="textandvalueSET" onchange="ShowDropTextValue()">

</select>
</body>
</html>