In my controller code contain html code in appened form.When i pass the parameters to the onclick function, I didn't get the parameters in the corresponding function.
controller
foreach ($cart as $item){
$row_id = $item['rowid'];
// $count++;
$output.='
<tr>
<td>'.$item['name'].'</td>
<td>'.$item['price'].'</td>
<td>'.$item['qty'].'</td>
<td>'.number_format($item['subtotal'],2).'</td>
<td> <a href="" onclick="remove("'.$row_id.'")" class="item-remove"><i class="zmdi zmdi-close"></i></a></td>
</tr>
';
}
script
function remove(row_id)
{
alert(row_id);
}
Onclick function remove(), alert is not working
In my controller code contain html code in appened form.When i pass the parameters to the onclick function, I didn't get the parameters in the corresponding function.
controller
foreach ($cart as $item){
$row_id = $item['rowid'];
// $count++;
$output.='
<tr>
<td>'.$item['name'].'</td>
<td>'.$item['price'].'</td>
<td>'.$item['qty'].'</td>
<td>'.number_format($item['subtotal'],2).'</td>
<td> <a href="" onclick="remove("'.$row_id.'")" class="item-remove"><i class="zmdi zmdi-close"></i></a></td>
</tr>
';
}
script
function remove(row_id)
{
alert(row_id);
}
Onclick function remove(), alert is not working
Share Improve this question edited Jul 10, 2017 at 13:06 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jul 10, 2017 at 10:57 robinsrobins 1,6687 gold badges34 silver badges71 bronze badges 5- why you are not using jquery..? – Dharmendra Singh Commented Jul 10, 2017 at 11:03
-
Try changing
remove("'.$row_id.'")
toremove('.$row_id.')
, that should work. – Milan Chheda Commented Jul 10, 2017 at 11:04 - its not working – robins Commented Jul 10, 2017 at 11:06
- if you use remove(2) it works..? – Masum billah Commented Jul 10, 2017 at 11:08
- @DharmendraSingh You've just settled a bet :P – Flosculus Commented Jul 10, 2017 at 11:38
2 Answers
Reset to default 5your old code is producing
<td> <a href="" onclick="remove("1")" class="item-remove"><i class="zmdi zmdi-close"></i></a></td>
which is an incorrect HTML
just replace this
onclick="remove("'.$row_id.'")"
with this
onclick="remove(\''.$row_id.'\')"
see a demo : https://eval.in/830107
onclick="remove("'.$row_id.'")"
Will result in:
onclick="remove("123")"
See where its going wrong? Onclick now contains only the portion remove(, because than a double quote termintes the onclick content.