Conor Caslin
Conor Caslin

Reputation: 83

html- change link on button from javascript function?

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">

Upvotes: 0

Views: 7198

Answers (2)

federicot
federicot

Reputation: 12341

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') {;
                                        ^

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

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';
   }
}

Upvotes: 1

Related Questions