0

Its quite wierd . My structure is

<form name="fm"> 
    <input type="file" name="fl">
    <input type="text" name="st"> 
</form>

Now document.fm.st.hidden=true; works as expected hiding the text box but document.fm.fl.hidden=true; doesnt works , it doesnt hides the file upload element ! Why & how to work around ?

3
  • It works jsfiddle.net/tajuhdvk. Please add more details. Commented Mar 29, 2016 at 10:58
  • When are you loading and running your script? Commented Mar 29, 2016 at 10:58
  • @The_Grits : I have writtten JS in end of file in '<script></script>' Commented Mar 29, 2016 at 10:59

4 Answers 4

1

It sounds like this might be a case of the script running before the page is completely loaded. If you want to be absolutely sure that the page is loaded before the script runs, you can do something like this:

document.onload = function(){ // if you want it could also be window.onload
    document.fm.st.hidden = true;
    document.fm.fl.hidden = true;
};
Sign up to request clarification or add additional context in comments.

Comments

0

Cant you just give it an id and use

document.getElementById('yourID');

other whise maybe you can make a css class for it and use Jquery

.addClass() and .removeClass()

1 Comment

and in case using the first option. Just use document.getElementById('yourID').style.display = none;
0

if you want to keep the input's space than apply visibility = "hidden" otherwise display = "none".

get Element by name var inputElements = document.getElementsByName("fl"); This will provide you a list of the elements which have name 'fl'.

Now if you want to hide all the elements, run a loop. For now, it'll provide only an element. var inputElement = inputElements[0];

I would suggest using jQuery if you want to keep using javascript for long.

now hide the element inputElement.style.display = "none";

Comments

0

For starters, make sure your script is after your element. The javascript you provided should work as you provided it, but if it doesn't, try the following:

<form name="fm"> 
    <input type="file" name="fl">
    <input type="text" name="st"> 
</form>

<script>
    // hides the element (it will still take up space)
    document.getElementsByName("fl").style.visibility="hidden";

    // hides the element (it does not take up any space)
    document.getElementsByName("fl").style.display="none";
</script>

Comments

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.