Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. How to call function second time?

How to call function second time?

Scheduled Pinned Locked Moved JavaScript
questiondata-structurestutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Rathore
    wrote on last edited by
    #1

    I have coding of a quiz with an array which has questions and options. First time the function is called successfully but when restarting the quiz, the function is not invoked. This is the code which is not working:

    $(document).on('click', '#restart', function() {
    showQuestions();
    });

    The whole coding is: // Showing questions and options

    var x = "";
    var i = 0;

    function showQuestions() {
    document.getElementById('holder').
    innerHTML = x;

    for (j = 0; j < words[i].options.length; j++) {
    if (i < words.length) {
    $('#holder').append("

    " +
    words[i].options[j] + "

    ");
    }
    }
    if (i < words.length) {
    $('#holder').append('

    ' +
    words[i].question + '

    ');
    i++;
    }
    }

    showQuestions();

    // Calling function on click of alternatives

    $('#holder').on('click',
    '.alternatives', function() {
    showQuestions();
    });

    // Checking answers

    score = 0;
    k = 0;
    

    $(document).on('click',
    '.alternatives', function() {
    let guess = this.id;
    if (k < words.length) {
    var correct = words[k].answer;
    k++;
    }
    if (guess === correct) {
    score++;
    }

     if (score <= words.length) {
         z = score;
         document.getElementById('summary').innerHTML = z;
     }
    
     if (k >= words.length) {
         $('#holder').fadeOut();
         $('#restart').fadeIn();
     }
    

    });

    // Restarting Quiz

    $(document).on('click', '#restart', function() {
    $(this).fadeOut();
    $('#holder').fadeIn();
    showQuestions();
    });

    L S 2 Replies Last reply
    0
    • T Tarun Rathore

      I have coding of a quiz with an array which has questions and options. First time the function is called successfully but when restarting the quiz, the function is not invoked. This is the code which is not working:

      $(document).on('click', '#restart', function() {
      showQuestions();
      });

      The whole coding is: // Showing questions and options

      var x = "";
      var i = 0;

      function showQuestions() {
      document.getElementById('holder').
      innerHTML = x;

      for (j = 0; j < words[i].options.length; j++) {
      if (i < words.length) {
      $('#holder').append("

      " +
      words[i].options[j] + "

      ");
      }
      }
      if (i < words.length) {
      $('#holder').append('

      ' +
      words[i].question + '

      ');
      i++;
      }
      }

      showQuestions();

      // Calling function on click of alternatives

      $('#holder').on('click',
      '.alternatives', function() {
      showQuestions();
      });

      // Checking answers

      score = 0;
      k = 0;
      

      $(document).on('click',
      '.alternatives', function() {
      let guess = this.id;
      if (k < words.length) {
      var correct = words[k].answer;
      k++;
      }
      if (guess === correct) {
      score++;
      }

       if (score <= words.length) {
           z = score;
           document.getElementById('summary').innerHTML = z;
       }
      
       if (k >= words.length) {
           $('#holder').fadeOut();
           $('#restart').fadeIn();
       }
      

      });

      // Restarting Quiz

      $(document).on('click', '#restart', function() {
      $(this).fadeOut();
      $('#holder').fadeIn();
      showQuestions();
      });

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You could make your question clearer by using <pre> tags* around the code block, indenting properly, and removing all those redundant blank lines, so it looks like:

      $(document).on('click', '#restart', function() {
      $(this).fadeOut();
      $('#holder').fadeIn();
      showQuestions();
      });

      *or use the code button at the top of the edit window.

      1 Reply Last reply
      0
      • T Tarun Rathore

        I have coding of a quiz with an array which has questions and options. First time the function is called successfully but when restarting the quiz, the function is not invoked. This is the code which is not working:

        $(document).on('click', '#restart', function() {
        showQuestions();
        });

        The whole coding is: // Showing questions and options

        var x = "";
        var i = 0;

        function showQuestions() {
        document.getElementById('holder').
        innerHTML = x;

        for (j = 0; j < words[i].options.length; j++) {
        if (i < words.length) {
        $('#holder').append("

        " +
        words[i].options[j] + "

        ");
        }
        }
        if (i < words.length) {
        $('#holder').append('

        ' +
        words[i].question + '

        ');
        i++;
        }
        }

        showQuestions();

        // Calling function on click of alternatives

        $('#holder').on('click',
        '.alternatives', function() {
        showQuestions();
        });

        // Checking answers

        score = 0;
        k = 0;
        

        $(document).on('click',
        '.alternatives', function() {
        let guess = this.id;
        if (k < words.length) {
        var correct = words[k].answer;
        k++;
        }
        if (guess === correct) {
        score++;
        }

         if (score <= words.length) {
             z = score;
             document.getElementById('summary').innerHTML = z;
         }
        
         if (k >= words.length) {
             $('#holder').fadeOut();
             $('#restart').fadeIn();
         }
        

        });

        // Restarting Quiz

        $(document).on('click', '#restart', function() {
        $(this).fadeOut();
        $('#holder').fadeIn();
        showQuestions();
        });

        S Offline
        S Offline
        Shraddha_Patel
        wrote on last edited by
        #3

        You can call this function on restart button onClick event... function restartquiz(){ $(document).on('click', function() { showQuestions(); }); }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups