0

Users will only be able to click the submit button if users select a video file and the textbox is not empty. i can disable the submit button but i cant re-enable it

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file"><span></span></label>
<input type="file" name="videofile" id="videofile" />
<br/>
Please enter video title:
<br/>
<input type"text" name="videoTitle" />
<br />
<input type="button" disabled id="Submit" value="Submit" onclick="submit()"/>
</form>

                <script>

                document.getElementById('submit').disabled = true;
                var n=document.getElementById('videoTitle').value;

                function submit();

                if($('#videofile')[0].files.length != 0) && (n.length > 1)
                {
                document.getElementById('submit').disabled = false;
                }
                </script>

enter image description here

0

5 Answers 5

2

Your code had a lot of mistakes. This is your corrected code:

function check() {
  if (document.querySelector('#videofile').files.length != 0 && (document.getElementById("videoTitle").value.length > 1)) {
    document.getElementById('Submit').disabled = false;
  } else {
    document.getElementById('Submit').disabled = true;
  }
}

Here is the JSFiddle demo

  • Missing ID added (u added only name attribute, and forgot the ID while still trying to use them)
  • Changed 'submit' in code to 'Submit'
  • Remove JQuery $ syntax
  • Validation on keypress rather than on submit click (which doesn't makes sense since u cant click the button while its disabled)
  • Else code added to re-disable the button if conditions are not valid
Sign up to request clarification or add additional context in comments.

Comments

1

onclick event over submit button does not make sense here. You must listen change event of the file input and keyup event of the text input and apply conditions accordingly. You have not assigned id attribute as well(videoTitle)

Also note that click handler will not be invoked if the button is disabled

Try this:

 var submitBtn = document.getElementById('Submit');

 function enableDisable() {
   var elem = document.getElementById('videofile');
   var n = document.getElementById('videoTitle').value;
   if (elem.files.length && n) {
     submitBtn.disabled = false;
   } else {
     submitBtn.disabled = true;
   }
 }
<form action="upload.php" method="post" enctype="multipart/form-data">
  <label for="file"><span></span></label>
  <input type="file" name="videofile" id="videofile" onchange='enableDisable()' />
  <br/> Please enter video title:
  <br/>
  <input type="text" name="videoTitle" id='videoTitle' onkeyup='enableDisable()' />
  <br />
  <input type="button" disabled id="Submit" value="Submit" />
</form>

2 Comments

just encounter some problem/errors using yr html code.. when posting the video file to the upload.php server i am getting Notice: Undefined index: file however when i use my original html form there is no error..
added screenshot of my code. cant paste it here as its too long. Undefined index: error on line 4,9,11,13,17,18,19,20,21
0

Can you try to make your submit function do something?

function submit() {
    if($('#videofile')[0].files.length != 0) && (n.length > 1) {
        document.getElementById('submit').disabled = false;
    }
};

Comments

0

you should use change event for text and videofile elements. So that whenever you modify something, change event is triggered and ur condition is checked inside it. Submit button is enable according to it.

Se the below code for reference!

    <form action="upload.php" method="post" enctype="multipart/form-data">
    <label for="file"><span></span></label>
    <input type="file" name="videofile" id="videofile" />
    <br/>
    Please enter video title:
    <br/>
    <input type"text" name="videoTitle" id="videoText"/>
    <br />
    <input type="button" disabled id="Submit" value="Submit" onclick="submit()"/>
    </form>

                    <script>

                    document.getElementById('submit').disabled = true;
                    var n=document.getElementById('videoTitle').value;

                    function submit();

    $('#videofile').change(function() {
          if($('#videofile')[0].files.length != 0) && (n.length > 1) {
              document.getElementById('submit').disabled = false;
          } else {
              document.getElementById('submit').disabled = true;
          }
    });

    $('#videoText').change(function() {
          if($('#videofile')[0].files.length != 0) && (n.length > 1) {
              document.getElementById('submit').disabled = false;
          } else {
              document.getElementById('submit').disabled = true;
          }
    });

                    </script>

Comments

0

I feel this is a bit simpler way to achieve this

<script>
    document.getElementById("submit").style.pointerEvents = "none"; //Disable
    var n=document.getElementById('videoTitle').value;

    function submit();

        if($('#videofile')[0].files.length != 0) && (n.length > 1)
        {
            document.getElementById("submit").style.pointerEvents = "all"; //Enable
        }
</script>

Just replace your script with this one & try..

Hope this works for you..

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.