JavaEar 专注于收集分享传播有价值的技术资料

Hiding a Textfield and its Label in Javascript

I have two textfields, one called label and one called link. I want that when an user hits control-(dash), he only gets the label textfield. However, when I try to hide the textfield, only the textfield disappears and the label of link stays around. I can't use JQuery, so I haven't seen a solution here, if I miss it let me know.

Here is my HTML:

<form id='linkEditDiv'>
  <table>
   <tr><th>Label:</th><td> <input type='text'  name='label'></td></tr>
   <tr><th>Link:</th><td> <input type='text' name='link'></td></tr> 
 </table>
</form>

Here is my Javascript:

var key=[];
        document.onkeydown = checkKey;
            function checkKey(e) 
            {
                e = e || window.event;
                 key[e.keyCode] = e.type == 'keydown';
        if (key[17]&&key[189])//checks if the control key and the dash key was hit
                {   

                        key=[];
                        document.getElementById('linkEditDiv').style.display='block';//if the textfield was already invisible 
                        document.getElementsByName('link')[0].style.display='none';

1个回答

    最佳答案
  1. Horrible code, but put the name in the tr and it will hide everything.

    HTML

    <tr name= 'link'><th>Link:</th><td> <input type='text'></td></tr>
    

    JS

    document.getElementsByName('link')[0].style.display='none'
    

    Note: This example uses your code and is not how I would do it. jQuery is your friend.

    Edit: Reverted back to first version if that's what you need, but I wouldn't put a name attribute in a tr tag, you're better off using ID.