0

OVERVIEW:

I'm coding a HTML page to display a dropdown menu and based on the option selected some input fields will be enabled and some disabled, but to do this I used the value field for (value="production_report"), and therefore I can't use it to add the line (value="index.html"), so when my button "build report" is clicked it loads the link (index.html).

QUESTION:

so I'm adding into my JavaScript function an onClick method to load the link when the button is pressed in my HTML form. So my question is, Is it possible to use an onClick() function like this???(Code below), and how would I do this???, I've tried the below. Any help would be much appreciated thanks.

JavaScript FUNCTION:|
---------------------
if (comp.value == 'production_report') {;          
        document.getElementById("build").onclick("location.href='index.html'");
}
else if (comp.value == 'please_select') {;
       document.getElementById("build").onclick("location.href='home.html'");
}

HTML Button code: |
-----------------
<input type="button" value="Build Report" onclick="???" id="build">

2 Answers 2

1

You need an actual function to execute onclick

if (comp.value == 'production_report') {;          
    document.getElementById("build").onclick = function() {
        location.href='index.html';
    }
}
else if (comp.value == 'please_select') {;
   document.getElementById("build").onclick = function() {
       location.href='home.html';
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Actually, the syntax is incorrect. It should be

document.getElementById('build').onclick = function () {
    location.href = THE LOCATION;
}

Also, you have a semi-colon after the opening bracket of the if statement, which will cause your script to fail.

if (comp.value == 'production_report') {;
                                        ^

1 Comment

Thanks for that fedeetz. The semi-colon was just a typo, but I did miss it, thank you for that also. Works perfect. :)

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.