0

I am learning JavaScript and I do not understand how to pass an ID from html into a JavaScript function.

My CSS page has this here:

#quizclock (with properties here)

And on my HTML page I have a javascript function as so:

<script type="text/javascript">
    var seconds = 0;
    var clockId;

    function runClock()
    {
    seconds + 1;
    quizclock = seconds;  //right here is my problem.
    }

    function startClock()
    {
    showQuiz();
    runClock();
    setInterval("runClock()", 1000);
    }

    function stopClock()
    {
    clearInterval(runClock);
    gradeQuiz();
    return = correctAns;
    alert("You have " + correctAns + " correct out of 5 in " + quizclock + " seconds.");
    }
</script>

So I need to use the id quizclock in the function. Any tips?

3

2 Answers 2

1

I noticed a few other problems with your code, I've commented the fixes and added a couple of other tips too.

var seconds = 0;
var clockId;
var correctAns;

// Lets get a reference to the quizclock element and save it in
// a variable named quizclock
var quizclock = document.getElementById('quizclock');

function runClock() {
    // seconds + 1;
    // This calculates seconds + 1 and then throws it away,
    // you need to save it back in to the variable
    // You could do that with:
    // seconds = seconds + 1;
    // But it would be even better with the shorthand:
    seconds += 1;

    // set the HTML inside of the quizclock element to new time
    quizclock.innerHTML = seconds;
}

function startClock() {
    showQuiz();
    runClock();

    // setInterval("runClock()", 1000);
    // When using setInterval and setTimeout you generally just
    // want to directly pass it the function by name. Passing it
    // a string "runClock()" is in effect actually running
    // eval("runClock()"), eval should be avoided unless you
    // really need it.
    // setInterval returns a number which identifies the interval,
    // you need to save that number, you'll need it when you
    // call clearInterval
    clockId = setInterval(runClock, 1000);
}

function stopClock() {
    // clearInterval takes the id that setInterval
        // returned to clear the interval
    clearInterval(clockId);

    gradeQuiz();

    // you had this alert statment after the return statement,
    // it would have never run, return statements end the
    // function and anything after them is ignored
    alert("You have " + correctAns + " correct out of 5 in " +
        quizclock + " seconds.");

    //return = correctAns;
    // the return statement doesn't need a =,
    // return = correctAns says set a variable named return to the
    // value of correctAns since return is a reserved word,
    // that should generate an error
    return correctAns;
}

Some useful reference links:

If this is for a formal class you might have to just use basic DOM methods to get elements (getElementById, etc). If you are just learning on your own I would encourage you to learn a DOM library. I would suggest jQuery, it is easy to learn and is now more or less the de facto standard. With jQuery instead of document.getElementById('quizclock') you could just do this: $('#quizclock'). Using jQuery makes your code a little shorter, standardizes things between different browsers and helps protect you from bugs in those browsers.

You are just a beginner now, in small examples like this you don't need to worry about global variables, but you should know that it is generally a bad idea to use too many of them. What if another function on the page also used a global variable named seconds? It might change seconds and screw up your timer. This is getting a little advance, but one way to avoid this is to wrap your code in a self-invoking anonymous function:

(function () {
    var seconds = 0;

    // inside here seconds is visible and can be used

}());

// outside seconds is not declared, it will return undefined.

Unfortunately any functions inside will also not be visible on the outside, so attaching them via onclick= wouldn't work but you could (should) attach them in using the DOM:

var submitButton = document.getElementById('submitanswers'); // you'll have to give the button an id
submitButton.addEventListener('click', stopClock, false);

Again, using jQuery would make this even easier:

$('#submitanswers').on('click', stopClock);

Likewise if you use jQuery, it already forces you to wrap your code in a function which will keep your variables out of the globalnamespace:

$(document).ready(function () {
    var seconds;
    // again seconds is visible here
});

// but not here
Sign up to request clarification or add additional context in comments.

6 Comments

This helped out a lot. However, my timer still will not run and there is no alert box popping up after I submit my results, which there should be.
Are you calling stopClock anywhere in the rest of your code? I'm assuming you should be calling it where you are "submitting" your results. Did you move the alert to before the return statement?
Is gradeQuiz supposed to return anything, such as the number of correct items? as is correctAns isn't being populated, it should either be returned by gradeQuiz and stored into correctAns or you should declare var correctAns in the global scope near var seconds, then gradeQuiz and `stopClock would both have access to it.
The stop clock is here: <input type="button" value="Submit Answers" onclick="stopClock()" /> and yes the alert is before the return statement.
gradeQuiz should store the value returned by the function in correctAns so yes.
|
0

You can select an element with:

var quizclock = document.getElementById('quizclock');

You can then set the value with:

quizclock.innerHTML = seconds;

1 Comment

you misspelled "quizclock"

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.