0

I have following code:

var storedNames = JSON.parse(localStorage.name); 
var mytable = "<table> cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>"; 
for (var i = 0; i < storedNames.length; i++) {   
  if (i % 2 == 1 && i != 1) {
     mytable += "</tr><tr>";   
  }

    mytable += "<td>" + storedNames[i].name +" "+ storedNames[i].email+"</td><td><img id='arrow' src='arrow.png' height='20' width='20' onclick='redirectToDetail();' ></td></tr>"; 
} 

mytable += "</tbody></table>";

document.write(mytable);

here, in redirectToDetail function i want to i value . How can I pass this? Any idea? thank u in advance

bipen
  • 36,319
  • 9
  • 49
  • 62

4 Answers4

1

try this

 mytable += "<td>" + storedNames[i].name +" "+ storedNames[i].email+"</td><td><img id='arrow' src='arrow.png' height='20' width='20' onclick='redirectToDetail(\'"+ i +"\');' ></td></tr>";

and take that in redirectToDetail

 function redirectToDetail(val){
     alert(val);
  }
bipen
  • 36,319
  • 9
  • 49
  • 62
0

try this

onclick='redirectToDetail(\'"+i+"\')
muneebShabbir
  • 2,500
  • 4
  • 29
  • 46
0

I think this will do. check it.

mytable += "<td>" + storedNames[i].name +" "+ storedNames[i].email+"</td><td><img id='arrow' src='arrow.png' height='20' width='20' onclick='redirectToDetail(" + i + ");' ></td></tr>";
999k
  • 6,257
  • 2
  • 29
  • 32
0

Just add the value as a literal constant that is passed to the onclick event and you are home free. In addition, I would strongly encourage you to consider using an array and joining the strings as you will yield much better performance particularly in IE.

var storedNames = JSON.parse(localStorage.name); 
var mytable = ['<table cellpadding="0" cellspacing="0"><tbody><tr>'];
for (var i = 0; i < storedNames.length; i++) {   
  if (i % 2 == 1 && i != 1) {
     mytable.push("</tr><tr>");   
  }
  mytable.push('<td>' + storedNames[i].name + ' ' + storedNames[i].email + '</td><td><img id="arrow" src="arrow.png" height="20" width="20" onclick="redirectToDetail(' + i + '"></td></tr>"');
} 

mytable.push("</tbody></table>");

document.write(mytable.join());

Now you can declare you onclick function like so:

function redirectToDetail(value) {
   //do stuff with the value
}
Matthew Cox
  • 13,566
  • 9
  • 54
  • 72