robins
robins

Reputation: 1668

How to pass string parameter through onclick

I'm generating a table through ajax function. But I need to add onclick function.Inside this function passing two parameters one is number and other is name.

Controller

 $table.='  <td><input type="checkbox"  value="" id="sp_'.$app_id.'" onclick="SpDetails('.$app_id.','.$app_details->app_name.');" /></td>'

 echo  $table;

script

 function SpDetails(val,name)
 {
    alert(name)

 }

But I didn't get the name when I alert.

Upvotes: 0

Views: 2635

Answers (3)

Mamdouh Saeed
Mamdouh Saeed

Reputation: 2324

  • You have extra " here id="sp_'.$app_id.'" ".
  • Pass the second parameter as string so you miss double quotes.
  • Internal double quotes should be escaped like this "foo \" foo".
  • You can pass variable in strings with double quotes "$variable".

so your code should be like this

   $table.="  <td><input type='checkbox'  value='' id='sp_$a' onclick='SpDetails($a,\"$b\");' /></td>";


     echo $table;

Upvotes: 1

Jack jdeoel
Jack jdeoel

Reputation: 4584

your parameter has no quote so it throws error . Use \' for making parameter as a string .

$table ='<td><input type="checkbox"  value="" id="sp_'.$app_id.'" onclick="SpDetails(\''.$app_id.'\',\''.$app_details->app_name.'\');" /></td>';

That will write in html as

<input onclick="SpDetails('app_id','app_name');"> //that is just example 

Your orginal is

<input onclick="SpDetails(app_id,app_name);">  //no quote so assume as variable

Upvotes: 2

Mohammad Akbari
Mohammad Akbari

Reputation: 4766

try following:

 onclick="SpDetails('" .$app_id. "','" .$app_details->app_name. "');"

Upvotes: 0

Related Questions