Disable button after adding task to task list









up vote
0
down vote

favorite












I would like to add some items to task list and disable button each time. When page loads it works fine.



I'd like to also disable button after adding each task.



If you add a new task and press submit button it works fine. But if the user choose pressing 'Enter' button instead of submit it becomes enabled.



What should it be done to disable submit button if the user prefers 'Enter' button instead of submit button ?



<!DOCTYPE html>
<html>
<head>
<script>
document.addEventListener('DOMContentLoaded', () =>

// By default, submit button is disabled
document.querySelector('#submit').disabled = true;

// Enable button only if there is text in the input field
document.querySelector('#task').onkeyup = () =>
document.querySelector('#submit').disabled = false;
;

document.querySelector('#new-task').onsubmit = () =>

// Create new item for list
const li = document.createElement('li');
li.innerHTML = document.querySelector('#task').value;

// Add new item to task list
document.querySelector('#tasks').append(li);

// Clear input field and disable button again
document.querySelector('#task').value = '';
document.querySelector('#submit').disabled = true;

// Stop form from submitting
return false;
;

);
</script>
<title>Tasks</title>
</head>


Body part of the html.



 <body>
<h1>Tasks</h1>
<ul id="tasks">
</ul>
<form id="new-task">
<input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
<input id="submit" type="submit">
</form>
</body>
</html>









share|improve this question





















  • What is Enter button here?
    – front_end_dev
    Nov 10 at 14:26










  • Enter button on the keyboard.
    – Erdem
    Nov 10 at 14:28






  • 1




    Try using set/get Attribute? developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
    – Marc Newton
    Nov 10 at 14:33















up vote
0
down vote

favorite












I would like to add some items to task list and disable button each time. When page loads it works fine.



I'd like to also disable button after adding each task.



If you add a new task and press submit button it works fine. But if the user choose pressing 'Enter' button instead of submit it becomes enabled.



What should it be done to disable submit button if the user prefers 'Enter' button instead of submit button ?



<!DOCTYPE html>
<html>
<head>
<script>
document.addEventListener('DOMContentLoaded', () =>

// By default, submit button is disabled
document.querySelector('#submit').disabled = true;

// Enable button only if there is text in the input field
document.querySelector('#task').onkeyup = () =>
document.querySelector('#submit').disabled = false;
;

document.querySelector('#new-task').onsubmit = () =>

// Create new item for list
const li = document.createElement('li');
li.innerHTML = document.querySelector('#task').value;

// Add new item to task list
document.querySelector('#tasks').append(li);

// Clear input field and disable button again
document.querySelector('#task').value = '';
document.querySelector('#submit').disabled = true;

// Stop form from submitting
return false;
;

);
</script>
<title>Tasks</title>
</head>


Body part of the html.



 <body>
<h1>Tasks</h1>
<ul id="tasks">
</ul>
<form id="new-task">
<input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
<input id="submit" type="submit">
</form>
</body>
</html>









share|improve this question





















  • What is Enter button here?
    – front_end_dev
    Nov 10 at 14:26










  • Enter button on the keyboard.
    – Erdem
    Nov 10 at 14:28






  • 1




    Try using set/get Attribute? developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
    – Marc Newton
    Nov 10 at 14:33













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I would like to add some items to task list and disable button each time. When page loads it works fine.



I'd like to also disable button after adding each task.



If you add a new task and press submit button it works fine. But if the user choose pressing 'Enter' button instead of submit it becomes enabled.



What should it be done to disable submit button if the user prefers 'Enter' button instead of submit button ?



<!DOCTYPE html>
<html>
<head>
<script>
document.addEventListener('DOMContentLoaded', () =>

// By default, submit button is disabled
document.querySelector('#submit').disabled = true;

// Enable button only if there is text in the input field
document.querySelector('#task').onkeyup = () =>
document.querySelector('#submit').disabled = false;
;

document.querySelector('#new-task').onsubmit = () =>

// Create new item for list
const li = document.createElement('li');
li.innerHTML = document.querySelector('#task').value;

// Add new item to task list
document.querySelector('#tasks').append(li);

// Clear input field and disable button again
document.querySelector('#task').value = '';
document.querySelector('#submit').disabled = true;

// Stop form from submitting
return false;
;

);
</script>
<title>Tasks</title>
</head>


Body part of the html.



 <body>
<h1>Tasks</h1>
<ul id="tasks">
</ul>
<form id="new-task">
<input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
<input id="submit" type="submit">
</form>
</body>
</html>









share|improve this question













I would like to add some items to task list and disable button each time. When page loads it works fine.



I'd like to also disable button after adding each task.



If you add a new task and press submit button it works fine. But if the user choose pressing 'Enter' button instead of submit it becomes enabled.



What should it be done to disable submit button if the user prefers 'Enter' button instead of submit button ?



<!DOCTYPE html>
<html>
<head>
<script>
document.addEventListener('DOMContentLoaded', () =>

// By default, submit button is disabled
document.querySelector('#submit').disabled = true;

// Enable button only if there is text in the input field
document.querySelector('#task').onkeyup = () =>
document.querySelector('#submit').disabled = false;
;

document.querySelector('#new-task').onsubmit = () =>

// Create new item for list
const li = document.createElement('li');
li.innerHTML = document.querySelector('#task').value;

// Add new item to task list
document.querySelector('#tasks').append(li);

// Clear input field and disable button again
document.querySelector('#task').value = '';
document.querySelector('#submit').disabled = true;

// Stop form from submitting
return false;
;

);
</script>
<title>Tasks</title>
</head>


Body part of the html.



 <body>
<h1>Tasks</h1>
<ul id="tasks">
</ul>
<form id="new-task">
<input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
<input id="submit" type="submit">
</form>
</body>
</html>






javascript ecmascript-6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 14:23









Erdem

3741520




3741520











  • What is Enter button here?
    – front_end_dev
    Nov 10 at 14:26










  • Enter button on the keyboard.
    – Erdem
    Nov 10 at 14:28






  • 1




    Try using set/get Attribute? developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
    – Marc Newton
    Nov 10 at 14:33

















  • What is Enter button here?
    – front_end_dev
    Nov 10 at 14:26










  • Enter button on the keyboard.
    – Erdem
    Nov 10 at 14:28






  • 1




    Try using set/get Attribute? developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
    – Marc Newton
    Nov 10 at 14:33
















What is Enter button here?
– front_end_dev
Nov 10 at 14:26




What is Enter button here?
– front_end_dev
Nov 10 at 14:26












Enter button on the keyboard.
– Erdem
Nov 10 at 14:28




Enter button on the keyboard.
– Erdem
Nov 10 at 14:28




1




1




Try using set/get Attribute? developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
– Marc Newton
Nov 10 at 14:33





Try using set/get Attribute? developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
– Marc Newton
Nov 10 at 14:33













3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










When you click the enter button, your onkeyup event handler changes the submit button disabled state to false, and the enter works.



Instead, listen to the input event of the #task box, and enable/disable the submit button according to the changes in the content. This will also handle the case in which submit is enabled after the text was deleted.



// Enable button only if there is text in the input field
document.querySelector('#task').addEventListener('input', (e) =>
document.querySelector('#submit').disabled = e.target.value === '';
);


Example:






// By default, submit button is disabled
document.querySelector('#submit').disabled = true;

// Enable button only if there is text in the input field
document.querySelector('#task').addEventListener('input', (e) =>
document.querySelector('#submit').disabled = e.target.value === '';
);

document.querySelector('#new-task').onsubmit = () =>

// Create new item for list
const li = document.createElement('li');
li.innerHTML = document.querySelector('#task').value;

// Add new item to task list
document.querySelector('#tasks').append(li);

// Clear input field and disable button again
document.querySelector('#task').value = '';
document.querySelector('#submit').disabled = true;

// Stop form from submitting
return false;
;

#submit:disabled 
opacity: 0.5;

<h1>Tasks</h1>
<ul id="tasks">
</ul>
<form id="new-task">
<input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
<input id="submit" type="submit">
</form>








share|improve this answer





























    up vote
    1
    down vote













    When you press enter key the event listner keyup is firing. You have to put the enable of button here in conditions



     document.querySelector('#task').onkeyup = (e) => 
    if(e.which === 13)
    return; // When user enter key press

    document.querySelector('#submit').disabled = false;
    ;





    share|improve this answer



























      up vote
      0
      down vote













      I was watching Harvard CS50 Web Programming Course and I'd like to share another solution. This is not a part of homework, assignment etc. so I feel free to share solution.



      Basically we enable button if there is text in the input field.



      // Enable button only if there is text in the input field
      document.querySelector('#task').onkeyup = () =>
      if (document.querySelector('#task').value.length > 0)
      document.querySelector('#submit').disabled = false;
      else
      document.querySelector('#submit').disabled = true;
      ;





      // By default, submit button is disabled
      document.querySelector('#submit').disabled = true;

      // Enable button only if there is text in the input field
      document.querySelector('#task').onkeyup = () =>
      if (document.querySelector('#task').value.length > 0)
      document.querySelector('#submit').disabled = false;
      else
      document.querySelector('#submit').disabled = true;
      ;

      document.querySelector('#new-task').onsubmit = () =>

      // Create new item for list
      const li = document.createElement('li');
      li.innerHTML = document.querySelector('#task').value;

      // Add new item to task list
      document.querySelector('#tasks').append(li);

      // Clear input field and disable button again
      document.querySelector('#task').value = '';
      document.querySelector('#submit').disabled = true;

      // Stop form from submitting
      return false;
      ;

      #submit:disabled 
      opacity: 0.5;

      <h1>Tasks</h1>
      <ul id="tasks">
      </ul>
      <form id="new-task">
      <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
      <input id="submit" type="submit">
      </form>








      share|improve this answer






















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239886%2fdisable-button-after-adding-task-to-task-list%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote



        accepted










        When you click the enter button, your onkeyup event handler changes the submit button disabled state to false, and the enter works.



        Instead, listen to the input event of the #task box, and enable/disable the submit button according to the changes in the content. This will also handle the case in which submit is enabled after the text was deleted.



        // Enable button only if there is text in the input field
        document.querySelector('#task').addEventListener('input', (e) =>
        document.querySelector('#submit').disabled = e.target.value === '';
        );


        Example:






        // By default, submit button is disabled
        document.querySelector('#submit').disabled = true;

        // Enable button only if there is text in the input field
        document.querySelector('#task').addEventListener('input', (e) =>
        document.querySelector('#submit').disabled = e.target.value === '';
        );

        document.querySelector('#new-task').onsubmit = () =>

        // Create new item for list
        const li = document.createElement('li');
        li.innerHTML = document.querySelector('#task').value;

        // Add new item to task list
        document.querySelector('#tasks').append(li);

        // Clear input field and disable button again
        document.querySelector('#task').value = '';
        document.querySelector('#submit').disabled = true;

        // Stop form from submitting
        return false;
        ;

        #submit:disabled 
        opacity: 0.5;

        <h1>Tasks</h1>
        <ul id="tasks">
        </ul>
        <form id="new-task">
        <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
        <input id="submit" type="submit">
        </form>








        share|improve this answer


























          up vote
          2
          down vote



          accepted










          When you click the enter button, your onkeyup event handler changes the submit button disabled state to false, and the enter works.



          Instead, listen to the input event of the #task box, and enable/disable the submit button according to the changes in the content. This will also handle the case in which submit is enabled after the text was deleted.



          // Enable button only if there is text in the input field
          document.querySelector('#task').addEventListener('input', (e) =>
          document.querySelector('#submit').disabled = e.target.value === '';
          );


          Example:






          // By default, submit button is disabled
          document.querySelector('#submit').disabled = true;

          // Enable button only if there is text in the input field
          document.querySelector('#task').addEventListener('input', (e) =>
          document.querySelector('#submit').disabled = e.target.value === '';
          );

          document.querySelector('#new-task').onsubmit = () =>

          // Create new item for list
          const li = document.createElement('li');
          li.innerHTML = document.querySelector('#task').value;

          // Add new item to task list
          document.querySelector('#tasks').append(li);

          // Clear input field and disable button again
          document.querySelector('#task').value = '';
          document.querySelector('#submit').disabled = true;

          // Stop form from submitting
          return false;
          ;

          #submit:disabled 
          opacity: 0.5;

          <h1>Tasks</h1>
          <ul id="tasks">
          </ul>
          <form id="new-task">
          <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
          <input id="submit" type="submit">
          </form>








          share|improve this answer
























            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            When you click the enter button, your onkeyup event handler changes the submit button disabled state to false, and the enter works.



            Instead, listen to the input event of the #task box, and enable/disable the submit button according to the changes in the content. This will also handle the case in which submit is enabled after the text was deleted.



            // Enable button only if there is text in the input field
            document.querySelector('#task').addEventListener('input', (e) =>
            document.querySelector('#submit').disabled = e.target.value === '';
            );


            Example:






            // By default, submit button is disabled
            document.querySelector('#submit').disabled = true;

            // Enable button only if there is text in the input field
            document.querySelector('#task').addEventListener('input', (e) =>
            document.querySelector('#submit').disabled = e.target.value === '';
            );

            document.querySelector('#new-task').onsubmit = () =>

            // Create new item for list
            const li = document.createElement('li');
            li.innerHTML = document.querySelector('#task').value;

            // Add new item to task list
            document.querySelector('#tasks').append(li);

            // Clear input field and disable button again
            document.querySelector('#task').value = '';
            document.querySelector('#submit').disabled = true;

            // Stop form from submitting
            return false;
            ;

            #submit:disabled 
            opacity: 0.5;

            <h1>Tasks</h1>
            <ul id="tasks">
            </ul>
            <form id="new-task">
            <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
            <input id="submit" type="submit">
            </form>








            share|improve this answer














            When you click the enter button, your onkeyup event handler changes the submit button disabled state to false, and the enter works.



            Instead, listen to the input event of the #task box, and enable/disable the submit button according to the changes in the content. This will also handle the case in which submit is enabled after the text was deleted.



            // Enable button only if there is text in the input field
            document.querySelector('#task').addEventListener('input', (e) =>
            document.querySelector('#submit').disabled = e.target.value === '';
            );


            Example:






            // By default, submit button is disabled
            document.querySelector('#submit').disabled = true;

            // Enable button only if there is text in the input field
            document.querySelector('#task').addEventListener('input', (e) =>
            document.querySelector('#submit').disabled = e.target.value === '';
            );

            document.querySelector('#new-task').onsubmit = () =>

            // Create new item for list
            const li = document.createElement('li');
            li.innerHTML = document.querySelector('#task').value;

            // Add new item to task list
            document.querySelector('#tasks').append(li);

            // Clear input field and disable button again
            document.querySelector('#task').value = '';
            document.querySelector('#submit').disabled = true;

            // Stop form from submitting
            return false;
            ;

            #submit:disabled 
            opacity: 0.5;

            <h1>Tasks</h1>
            <ul id="tasks">
            </ul>
            <form id="new-task">
            <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
            <input id="submit" type="submit">
            </form>








            // By default, submit button is disabled
            document.querySelector('#submit').disabled = true;

            // Enable button only if there is text in the input field
            document.querySelector('#task').addEventListener('input', (e) =>
            document.querySelector('#submit').disabled = e.target.value === '';
            );

            document.querySelector('#new-task').onsubmit = () =>

            // Create new item for list
            const li = document.createElement('li');
            li.innerHTML = document.querySelector('#task').value;

            // Add new item to task list
            document.querySelector('#tasks').append(li);

            // Clear input field and disable button again
            document.querySelector('#task').value = '';
            document.querySelector('#submit').disabled = true;

            // Stop form from submitting
            return false;
            ;

            #submit:disabled 
            opacity: 0.5;

            <h1>Tasks</h1>
            <ul id="tasks">
            </ul>
            <form id="new-task">
            <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
            <input id="submit" type="submit">
            </form>





            // By default, submit button is disabled
            document.querySelector('#submit').disabled = true;

            // Enable button only if there is text in the input field
            document.querySelector('#task').addEventListener('input', (e) =>
            document.querySelector('#submit').disabled = e.target.value === '';
            );

            document.querySelector('#new-task').onsubmit = () =>

            // Create new item for list
            const li = document.createElement('li');
            li.innerHTML = document.querySelector('#task').value;

            // Add new item to task list
            document.querySelector('#tasks').append(li);

            // Clear input field and disable button again
            document.querySelector('#task').value = '';
            document.querySelector('#submit').disabled = true;

            // Stop form from submitting
            return false;
            ;

            #submit:disabled 
            opacity: 0.5;

            <h1>Tasks</h1>
            <ul id="tasks">
            </ul>
            <form id="new-task">
            <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
            <input id="submit" type="submit">
            </form>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 14:36

























            answered Nov 10 at 14:31









            Ori Drori

            70.6k127286




            70.6k127286






















                up vote
                1
                down vote













                When you press enter key the event listner keyup is firing. You have to put the enable of button here in conditions



                 document.querySelector('#task').onkeyup = (e) => 
                if(e.which === 13)
                return; // When user enter key press

                document.querySelector('#submit').disabled = false;
                ;





                share|improve this answer
























                  up vote
                  1
                  down vote













                  When you press enter key the event listner keyup is firing. You have to put the enable of button here in conditions



                   document.querySelector('#task').onkeyup = (e) => 
                  if(e.which === 13)
                  return; // When user enter key press

                  document.querySelector('#submit').disabled = false;
                  ;





                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    When you press enter key the event listner keyup is firing. You have to put the enable of button here in conditions



                     document.querySelector('#task').onkeyup = (e) => 
                    if(e.which === 13)
                    return; // When user enter key press

                    document.querySelector('#submit').disabled = false;
                    ;





                    share|improve this answer












                    When you press enter key the event listner keyup is firing. You have to put the enable of button here in conditions



                     document.querySelector('#task').onkeyup = (e) => 
                    if(e.which === 13)
                    return; // When user enter key press

                    document.querySelector('#submit').disabled = false;
                    ;






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 14:32









                    front_end_dev

                    1,310411




                    1,310411




















                        up vote
                        0
                        down vote













                        I was watching Harvard CS50 Web Programming Course and I'd like to share another solution. This is not a part of homework, assignment etc. so I feel free to share solution.



                        Basically we enable button if there is text in the input field.



                        // Enable button only if there is text in the input field
                        document.querySelector('#task').onkeyup = () =>
                        if (document.querySelector('#task').value.length > 0)
                        document.querySelector('#submit').disabled = false;
                        else
                        document.querySelector('#submit').disabled = true;
                        ;





                        // By default, submit button is disabled
                        document.querySelector('#submit').disabled = true;

                        // Enable button only if there is text in the input field
                        document.querySelector('#task').onkeyup = () =>
                        if (document.querySelector('#task').value.length > 0)
                        document.querySelector('#submit').disabled = false;
                        else
                        document.querySelector('#submit').disabled = true;
                        ;

                        document.querySelector('#new-task').onsubmit = () =>

                        // Create new item for list
                        const li = document.createElement('li');
                        li.innerHTML = document.querySelector('#task').value;

                        // Add new item to task list
                        document.querySelector('#tasks').append(li);

                        // Clear input field and disable button again
                        document.querySelector('#task').value = '';
                        document.querySelector('#submit').disabled = true;

                        // Stop form from submitting
                        return false;
                        ;

                        #submit:disabled 
                        opacity: 0.5;

                        <h1>Tasks</h1>
                        <ul id="tasks">
                        </ul>
                        <form id="new-task">
                        <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
                        <input id="submit" type="submit">
                        </form>








                        share|improve this answer


























                          up vote
                          0
                          down vote













                          I was watching Harvard CS50 Web Programming Course and I'd like to share another solution. This is not a part of homework, assignment etc. so I feel free to share solution.



                          Basically we enable button if there is text in the input field.



                          // Enable button only if there is text in the input field
                          document.querySelector('#task').onkeyup = () =>
                          if (document.querySelector('#task').value.length > 0)
                          document.querySelector('#submit').disabled = false;
                          else
                          document.querySelector('#submit').disabled = true;
                          ;





                          // By default, submit button is disabled
                          document.querySelector('#submit').disabled = true;

                          // Enable button only if there is text in the input field
                          document.querySelector('#task').onkeyup = () =>
                          if (document.querySelector('#task').value.length > 0)
                          document.querySelector('#submit').disabled = false;
                          else
                          document.querySelector('#submit').disabled = true;
                          ;

                          document.querySelector('#new-task').onsubmit = () =>

                          // Create new item for list
                          const li = document.createElement('li');
                          li.innerHTML = document.querySelector('#task').value;

                          // Add new item to task list
                          document.querySelector('#tasks').append(li);

                          // Clear input field and disable button again
                          document.querySelector('#task').value = '';
                          document.querySelector('#submit').disabled = true;

                          // Stop form from submitting
                          return false;
                          ;

                          #submit:disabled 
                          opacity: 0.5;

                          <h1>Tasks</h1>
                          <ul id="tasks">
                          </ul>
                          <form id="new-task">
                          <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
                          <input id="submit" type="submit">
                          </form>








                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            I was watching Harvard CS50 Web Programming Course and I'd like to share another solution. This is not a part of homework, assignment etc. so I feel free to share solution.



                            Basically we enable button if there is text in the input field.



                            // Enable button only if there is text in the input field
                            document.querySelector('#task').onkeyup = () =>
                            if (document.querySelector('#task').value.length > 0)
                            document.querySelector('#submit').disabled = false;
                            else
                            document.querySelector('#submit').disabled = true;
                            ;





                            // By default, submit button is disabled
                            document.querySelector('#submit').disabled = true;

                            // Enable button only if there is text in the input field
                            document.querySelector('#task').onkeyup = () =>
                            if (document.querySelector('#task').value.length > 0)
                            document.querySelector('#submit').disabled = false;
                            else
                            document.querySelector('#submit').disabled = true;
                            ;

                            document.querySelector('#new-task').onsubmit = () =>

                            // Create new item for list
                            const li = document.createElement('li');
                            li.innerHTML = document.querySelector('#task').value;

                            // Add new item to task list
                            document.querySelector('#tasks').append(li);

                            // Clear input field and disable button again
                            document.querySelector('#task').value = '';
                            document.querySelector('#submit').disabled = true;

                            // Stop form from submitting
                            return false;
                            ;

                            #submit:disabled 
                            opacity: 0.5;

                            <h1>Tasks</h1>
                            <ul id="tasks">
                            </ul>
                            <form id="new-task">
                            <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
                            <input id="submit" type="submit">
                            </form>








                            share|improve this answer














                            I was watching Harvard CS50 Web Programming Course and I'd like to share another solution. This is not a part of homework, assignment etc. so I feel free to share solution.



                            Basically we enable button if there is text in the input field.



                            // Enable button only if there is text in the input field
                            document.querySelector('#task').onkeyup = () =>
                            if (document.querySelector('#task').value.length > 0)
                            document.querySelector('#submit').disabled = false;
                            else
                            document.querySelector('#submit').disabled = true;
                            ;





                            // By default, submit button is disabled
                            document.querySelector('#submit').disabled = true;

                            // Enable button only if there is text in the input field
                            document.querySelector('#task').onkeyup = () =>
                            if (document.querySelector('#task').value.length > 0)
                            document.querySelector('#submit').disabled = false;
                            else
                            document.querySelector('#submit').disabled = true;
                            ;

                            document.querySelector('#new-task').onsubmit = () =>

                            // Create new item for list
                            const li = document.createElement('li');
                            li.innerHTML = document.querySelector('#task').value;

                            // Add new item to task list
                            document.querySelector('#tasks').append(li);

                            // Clear input field and disable button again
                            document.querySelector('#task').value = '';
                            document.querySelector('#submit').disabled = true;

                            // Stop form from submitting
                            return false;
                            ;

                            #submit:disabled 
                            opacity: 0.5;

                            <h1>Tasks</h1>
                            <ul id="tasks">
                            </ul>
                            <form id="new-task">
                            <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
                            <input id="submit" type="submit">
                            </form>








                            // By default, submit button is disabled
                            document.querySelector('#submit').disabled = true;

                            // Enable button only if there is text in the input field
                            document.querySelector('#task').onkeyup = () =>
                            if (document.querySelector('#task').value.length > 0)
                            document.querySelector('#submit').disabled = false;
                            else
                            document.querySelector('#submit').disabled = true;
                            ;

                            document.querySelector('#new-task').onsubmit = () =>

                            // Create new item for list
                            const li = document.createElement('li');
                            li.innerHTML = document.querySelector('#task').value;

                            // Add new item to task list
                            document.querySelector('#tasks').append(li);

                            // Clear input field and disable button again
                            document.querySelector('#task').value = '';
                            document.querySelector('#submit').disabled = true;

                            // Stop form from submitting
                            return false;
                            ;

                            #submit:disabled 
                            opacity: 0.5;

                            <h1>Tasks</h1>
                            <ul id="tasks">
                            </ul>
                            <form id="new-task">
                            <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
                            <input id="submit" type="submit">
                            </form>





                            // By default, submit button is disabled
                            document.querySelector('#submit').disabled = true;

                            // Enable button only if there is text in the input field
                            document.querySelector('#task').onkeyup = () =>
                            if (document.querySelector('#task').value.length > 0)
                            document.querySelector('#submit').disabled = false;
                            else
                            document.querySelector('#submit').disabled = true;
                            ;

                            document.querySelector('#new-task').onsubmit = () =>

                            // Create new item for list
                            const li = document.createElement('li');
                            li.innerHTML = document.querySelector('#task').value;

                            // Add new item to task list
                            document.querySelector('#tasks').append(li);

                            // Clear input field and disable button again
                            document.querySelector('#task').value = '';
                            document.querySelector('#submit').disabled = true;

                            // Stop form from submitting
                            return false;
                            ;

                            #submit:disabled 
                            opacity: 0.5;

                            <h1>Tasks</h1>
                            <ul id="tasks">
                            </ul>
                            <form id="new-task">
                            <input id="task" autocomplete="off" autofocus placeholder="New Task" type="text">
                            <input id="submit" type="submit">
                            </form>






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 11 at 6:53

























                            answered Nov 11 at 6:43









                            Erdem

                            3741520




                            3741520



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239886%2fdisable-button-after-adding-task-to-task-list%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                                Museum of Modern and Contemporary Art of Trento and Rovereto