robins
robins

Reputation: 1668

How to pass parameters in onclick function of generated html

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

Upvotes: 2

Views: 13091

Answers (2)

Accountant م
Accountant م

Reputation: 7483

your 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

Upvotes: 5

Johan
Johan

Reputation: 929

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.

Upvotes: 1

Related Questions