javascript - making <td> visible if another <td> is clicked -
so have below, code can seen below:
<table> <tr> <td id="first">itemone</td> <td id="firstclicked">itemoneinfo</td> </tr> <tr> <td id="second">itemtwo</td> <td id="secondclicked">itemtwoinfo</td> </tr> </table> i want #firstclicked , #secondclicked hidden when #first , #second clicked, want #firstclicked , #secondclicked appear. how make happen? far have
$('#firstclicked').css('visiblilty','hidden'); $('#first').click(function() { $('#firstclicked').css('visibility','visible'); }); but isn't working. plus, if works, i'd have same thing #second, , i'm planning on creating more 's dont want repeat same code several times. there better way of doing this?
i'd recommend using jquery's hide , show functions, on function event handling. jquery's main power makes things "just work" across browsers, using hide let library choose in order make action happen, rather having second guess yourself. example:
$('#firstclicked').hide(); $('#first').on('click',function() { $('#firstclicked').show(); }); in addition, in original code, have few mistakes:
$('firstclicked').css('visiblilty','hidden'); // should be: $('#firstclicked').css('visibility','hidden'); however, you're worried having duplicate code, following:
html:
<table> <tr> <td id="first" class="clickable">itemone</td> <td id="firstclicked"><div class="initiallyhidden">itemoneinfo</div></td> </tr> <tr> <td id="second" class="clickable">itemtwo</td> <td id="secondclicked"><div class="initiallyhidden">itemtwoinfo</div></td> </tr> </table> js:
$('.initiallyhidden').hide(); $('.clickable').on('click',function() { $(this).siblings("td").children(".initiallyhidden").show(); }); this way element class clickable, when clicked, sibling class initiallyhidden , show it
edit: updated example deal issue raised in christophe's comment.
Comments
Post a Comment