0

Here is my code, I wanna get the element info in the click function like my annotation says.

for(var i = 0, len = json.length; i < length; i++) {
  var curid = json['success'][i]['courier_id'];
  var curname = json['success'][i]['courier_name'];
  document.getElementById("gsid").value=curname;                    
  var tr="<tr>&nbsp;&nbsp;";

  //here is where i want to use onclick function with curid and curname 
  var td1="<td>"+"<input type='radio' name='as' value='json['success'][i]['courier_id']' id='curiid' onclick='courierselect(<?php echo $shyam='\'+curname+\''; ?>);'/>"+"</td>&nbsp;&nbsp;";
  var td2="<td id='curnname'>"+json['success'][i]['courier_name']+"</td>&nbsp;&nbsp;";
  var td3="<td>"+json['success'][i]['country_charge']+"</td>&nbsp;&nbsp;";
  var td4="<td>"+json['success'][i]['estimate_delivery_time']+"</td>&nbsp;&nbsp;";
  var td5="<td>"+json['success'][i]['tracking_available']+"</td></tr>&nbsp;&nbsp;";
  $("#results").append(tr+td1+td2+td3+td4+td5); 
}
lucke84
  • 4,516
  • 3
  • 37
  • 58

1 Answers1

0

I see three problems on your code

  • ... value='json['success'][i]['courier_id']' ...

You didn't concact the variable with the string

  • ... courierselect(<?php echo $shyam='\'+curname+\''; ?>) ...

    1. echo $shyam='\'+curname+\''; you can't set a PHP variable with a JS value. Understand why
    2. You echo a string value, so you have to add some quotes around curname

for(var i = 0, len = json.length; i < length; i++) {
  var curid = json['success'][i]['courier_id'];
  var curname = json['success'][i]['courier_name'];
  document.getElementById("gsid").value = curname;

  $("#results").append('<tr>&nbsp;&nbsp;');
  $("#results").append('<td><input type="radio" name="as" value="' + curid + '" id="curiid" onclick="courierselect(\'' + curname + '\');"/></td>&nbsp;&nbsp;');
  $("#results").append('<td id="curnname">' + curname + '</td>&nbsp;&nbsp;');
  $("#results").append('<td>' + json['success'][i]['country_charge'] + '</td>&nbsp;&nbsp;');
  $("#results").append('<td>' + json['success'][i]['estimate_delivery_time'] + '</td>&nbsp;&nbsp;');
  $("#results").append('<td>' + json['success'][i]['tracking_available'] + '</td></tr>&nbsp;&nbsp;');
}
Community
  • 1
  • 1
Robiseb
  • 1,576
  • 2
  • 14
  • 17
  • Thanx you sir your code is working :) last one if i want to pass multiple variable than can i do this {courierselect(\'' + curname + '\',\'' + curid+ '\')} – shyamkhanna Nov 05 '16 at 10:06
  • Yes, you can : `onclick="courierselect(\'' + curname + '\',\'' + curid+ '\');`. If `curid` is _int_, the quotes are not necessary : `onclick="courierselect(\'' + curname + '\', curid);` – Robiseb Nov 05 '16 at 10:28