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

8
  • 1
    Where is your redirectToDetail function? Commented Apr 9, 2013 at 7:27
  • 1
    var mytable = "<table> cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>"; or var mytable = "<table cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>";? Commented Apr 9, 2013 at 7:32
  • 1
    @FeistyMango this is not answer, just wrong syntax notice. Commented Apr 9, 2013 at 7:35
  • 1
    @FeistyMango do we speak about same thing? :) Check symbol after <table Commented Apr 9, 2013 at 7:43
  • 1
    @Narek Ahh, I see what you mean. I thought you were making an issue of the quotes. Doh Commented Apr 9, 2013 at 7:45

4 Answers 4

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);
  }
Sign up to request clarification or add additional context in comments.

Comments

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
}

Comments

0

try this

onclick='redirectToDetail(\'"+i+"\')

Comments

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>";

2 Comments

Ok. I think you noticed this unwanted > in var mytable = "<table> cellpadding=\"0\"
Also in your code pairing issue for tag <tr>. For eg if storedNames.length == 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.