Javascript blues in PHP: getElementById & getElementsByClassName -


this should simple enough, alas it's giving me issues:

i have php page gives me 3 different iterations of td contents. first shown display: table-cell, , others hidden display: none.

echo '<td style="display: table-cell" class="gamelinecell" id="'.$playerline.'" colspan="3">'; 

&

echo '<td style="display: none" class="gamelinecell" id="'.$playerline.'" colspan="3">'; 

i have link corresponding each td option -- clicking supposed run function loadline pass string corresponds td id. id strings akin 'ev-f1' -- 5 characters, number, hyphen.

echo "<a onclick='loadline(".$playerline.")' href='javascript:void(0)'>".$playerline."</a>"; 

the script hides tds of class gamelinecell, , displays 1 link clicked.

<script>     function loadline(line) {         var lines = document.getelementsbyclassname('gamelinecell');         (var = 0; < lines.length; i++) {             lines[i].style.display = 'none';         }             document.getelementbyid(line).style.display = 'table-cell';     } </script> 

when view source of php page renders -- links show proper strings in loadline brackets, each of tds present, first showing , others hidden -- links not work. i've tried removing getelementsbyclassname sequence, running getelementbyid, though no avail. nothing happens.

any ideas?

much obliged help,

andrew


edit: source of error? javascript cutting strings! ev-f1 becomes ev. looking whys , how fix now.

there few issues here related proper escaping , enclosure of strings; use printf() print html, like:

printf('<td style="display: table-cell" class="gamelinecell" id="%s" colspan="3">',     htmlspecialchars($playerline, ent_quotes, 'utf-8'); ); 

this piece of code in particular tricky:

echo "<a onclick='loadline(".$playerline.")' href='javascript:void(0)'>".$playerline."</a>"; 

the problem $playerline doesn't contain javascript string enclosures , attempt resolve ev-f1 ev - f1 (i.e., subtraction of ev , f1). fix that, need encode variable using json , apply html escaping:

printf('<a onclick="loadline(%s)" href="javascript:void(0)">%s</a>',     htmlspecialchars(json_encode($playerline), ent_quotes, 'utf-8'),     htmlspecialchars($playerline), ent_quotes, 'utf-8') ); 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -