JavaScript
x
16
16
1
function takeaction(id,tr,i,form){
2
if (window.confirm("Do you want to mark this record ? ")) {
3
var form = getElementById(form);
4
var element = document.getElementById(tr);
5
element.classList.remove("table-danger");
6
element.classList.add("table-success");
7
var element = document.getElementById(id);
8
element.remove(id);
9
var element = document.getElementById(i);
10
11
element.classList.add("fa-check");
12
form.submit();
13
} else {
14
return 0;
15
}
16
}
JavaScript
1
19
19
1
<table class="table table-hover">
2
<thead class="thead-dark">
3
<h1>Notifications</h1>
4
<tr>
5
6
<th>Name</th>
7
<th>Action</th>
8
</tr>
9
</thead>
10
<tbody>
11
<tr id='tr4' class='table-danger'>
12
<td>Geethan</td>
13
<td>
14
<form action='include/action.php?user_id=4' method='post' id='form' >
15
<input id='tr4_btn' class='btn btn-warning' type='submit' name='action1' value='Take action' onclick="takeaction(this.id ,'tr4','tr4_i','form')" />
16
<i id="tr4_i" class="fa " style="color:green;"></i>
17
</form>
18
</td>
19
</tr>
when I tried to submit this form and change the class names by using js function “onclick=”takeaction(this.id ,’tr4′,’tr4_i’,’form’)” the form is submitted but class names are not changed. I want to do both these processes at once
Advertisement
Answer
JavaScript
1
18
18
1
function takeaction(id,tr,i,form){
2
if (window.confirm("Do you want to mark this record ? ")) {
3
var $this = jQuery('#'+id);
4
var $form = $this.parents('form');
5
// var form = getElementById(form);
6
var element = document.getElementById(tr);
7
element.classList.remove("table-danger");
8
element.classList.add("table-success");
9
var element = document.getElementById(id);
10
element.remove(id);
11
var element = document.getElementById(i);
12
element.classList.add("fa-check");
13
$form.submit();
14
//form.submit();
15
} else {
16
return 0;
17
}
18
}
JavaScript
1
39
39
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<!-- Latest compiled and minified CSS -->
5
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
6
7
<!-- jQuery library -->
8
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
9
10
<!-- Popper JS -->
11
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
12
13
<!-- Latest compiled JavaScript -->
14
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
15
</head>
16
<body>
17
<table class="table table-hover">
18
<thead class="thead-dark">
19
<h1>Notifications</h1>
20
<tr>
21
22
<th>Name</th>
23
<th>Action</th>
24
</tr>
25
</thead>
26
<tbody>
27
<tr id='tr4' class='table-danger'>
28
<td>Name</td>
29
<td>
30
<form action='include/action.php?user_id=4' method='post' id='form' >
31
<input id='tr4_btn' class='btn btn-warning' type='submit' name='action1' value='Take action' onclick="takeaction(this.id ,'tr4','tr4_i','form')" />
32
<i id="tr4_i" class="fa " style="color:green;"></i>
33
</form>
34
</td>
35
</tr>
36
</tbody>
37
</table>
38
</body>
39
</html>