1

How to not open the file upload box when input mytextField is null.

       <input type="text" id="mytextField">
    <input type="file" multiple id="myFileUpload">



    <script>
        document.getElementById("myFileUpload").addEventListener("click", function () {
    var myTextF = document.getElementById("mytextField");
    if(myTextF = null || myTextF == ''){
    //will not open the file upload
    } else {
    //let the file upload open
    }
    });
    </script>
1
  • 1
    If you found your solution in the answers, please mark the answer as the solution so the question can be closed. Commented May 15, 2020 at 10:08

2 Answers 2

1

You can disable the upload button when textarea is empty and enable it when textarea contains text.

<input type="text" onkeyup="checkField()" id="mytextField">
<input type="file" multiple id="myFileUpload" disabled="disabled">

script:

function checkField(){
    var myTextF = document.getElementById("mytextField");
    if(myTextF.value.trim() != ""){
        document.getElementById("myFileUpload").disabled = false;
    }
}

Used the trim() function to prevent empty whitespace texts.

Sign up to request clarification or add additional context in comments.

Comments

0

Here you are assigning myTextF to the element, rather than the innerText. Try with var myTextF = document.getElementById("myTextF").innerText;

If something doesn't work, you could always try to log the output to the console.

In this case, because you are assigning the element, that resolves as TRUE, and it will always try to open the file.

document.getElementById("myFileUpload").addEventListener("click", function () {
    var myTextF = document.getElementById("mytextField");
    if(myTextF = null || myTextF == ''){
    //will not open the file upload
    console.log("I should not open file")
    } else {
    //let the file upload open
    console.log("I should open the file")
    }
    });

" I should open the file"

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.