Skip to content

Instantly share code, notes, and snippets.

@wsdookadr
Created December 14, 2010 14:53
Show Gist options
  • Select an option

  • Save wsdookadr/740525 to your computer and use it in GitHub Desktop.

Select an option

Save wsdookadr/740525 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Value vs. Reference</title>
</head>
<body>
<table>
<tbody>
<tr>
<td><input type="button" value="to_input" class="lupa" /></td>
<td><span atr="a">1</span></td>
<td><span atr="b">2</span></td>
<td><span atr="c">3</span></td>
</tr>
<tr>
<td><input type="button" value="to_input" class="lupa" /></td>
<td><span atr="d">4</span></td>
<td><span atr="e">5</span></td>
<td><span atr="f">6</span></td>
</tr>
</tbody>
</table>
<br />
<script type="text/javascript">
function to_input(obj, index) {
console.log("inside i="+index+"\n");
var i = index;
var dims = obj.parentNode.parentNode.getElementsByTagName('td');//iterate over spans in that tr
var tmpVal, atr;
for(var j = 1; j < dims.length; j++) {//skip, first which is the td with button in it
atr = dims[j].getElementsByTagName('span')[0].getAttribute('atr');
tmpVal = dims[j].getElementsByTagName('span')[0].innerHTML;
console.log(atr+" "+i+" "+j+"\n");
dims[j].innerHTML = '<input type="text" name="' + atr + i+ j + '" value="' + tmpVal + '" />';
}
}
var anc = document.getElementsByClassName('lupa');
for (var i = 0; i < anc.length; i++) {// anc are the tds with button in them
console.log("before i="+i+"\n");
(function(){
var w = i;// w is i,
anc[i].onclick = function() {
to_input(this, w);
// if instead of w we use i, i is temporary inside the outer loop,
// and you will see 2 instead of 0 and 1 because at the end of the loop i is 2, the condition for the "for" loop to continue
// is i < 2 and when i == 2 this condition is false and the "for" loop stops.
};
})();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment