简体   繁体   中英

HTML & Javascript - Button not working

I'm trying to finish my final project for my introductory web development course (I'm very new to javascript and html), which is a simple Mad Libs project. I've been running into problems all day with trying to get it to work, and I think I've largely eradicated most of the bugs. I've really hit a roadblock though, with the button I'm using that is supposed to load in the results and call the function that builds the story using an event listener in the javascript file. When I run my program, the button doesn't do a thing. I checked the Google Chrome developer console, and I'm not getting any error messages or anything, so I have no clue what I'm doing wrong.

Here's my javascript file (named mad_libs_1.js):

var story = document.querySelector("#story");
var adjective1 = document.querySelector("#adjective1");
var verbEndingInED = document.querySelector("#verbEndingInED");
var nounPlural1 = document.querySelector("#nounPlural1");
var liquid = document.querySelector("#liquid");
var nounPlural2 = document.querySelector("#nounPlural2");
var famousPerson = document.querySelector("#famousPerson");
var place = document.querySelector("#place");
var occupation = document.querySelector("#occupation");
var noun1 = document.querySelector("#noun1");
var nationality = document.querySelector("#nationality");
var femaleCelebrity = document.querySelector("#femaleCelebrity");
var noun2 = document.querySelector("#noun2");
var femaleFriend = document.querySelector("#femaleFriend");
var nounPlural3 = document.querySelector("#nounPlural3");
var number = document.querySelector("#number");
var adjective2 = document.querySelector("#adjective2");

//event listener for the button
var finished = document.querySelector("#finished");
if(finished){
    finished.addEventListener("click", createStory, false);
}


function createStory(){
    console.log("createStory HAS BEEN CALLED");
    var finalStory = ""; 
    finalStory += "I enjoy long, " + adjective1.bold();
    finalStory += " walks on the beach, getting " + verbEndingInED.bold();
    finalStory += " in the rain and serendipitous encounters with " + nounPlural1.bold();
    finalStory += ". I really like piña coladas mixed with " + liquid.bold();
    finalStory += ", and romantic, candle-lit " + nounPlural2.bold();
    finalStory += ". I am well-read from Dr. Seuss to " + famousPerson.bold();
    finalStory += ". I travel frequently, especially to " + place.bold();
    finalStory += ", when I am not busy with work. (I am a " + occupation.bold();
    finalStory += ".) I am looking for " + noun.bold();
    finalStory += " and beauty in the form of a " + nationality.bold();
    finalStory += " goddess. She should have the physique of " + femaleCelebrity.bold();
    finalStory += " and the " + noun2.bold();
    finalStory += " of " + femaleFriend.bold();
    finalStory += ". I would prefer if she knew how to cook, clean, and wash my " + nounPlural3.bold();
    finalStory += ". I know I’m not very attractive in my picture, but it was taken " + number.bold();
    finalStory += " days ago, and I have since become more " + adjective2.bold() + ".";
    story.innerHTML = finalStory;
    console.log(story + " IS THE STORY");
}

and here's my html:

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Mad Libs 1</title>
        <script type="text/javascript" src="mad_libs_1.js"></script>
    </head>
    <body lang="en-US">
        <div class="container-fluid">
            <h1>Mad Libs 1</h1>
        </div>
        <div class="container-fluid">
            <ul>
                <li>Adjective: <input type="text" name="adjective1" id="adjective1"><br>
                <li>Verb Ending in "ED": <input type="text" name="verbEndingInED" id="verbEndingInED"><br>
                <li>Noun (Plural): <input type="text" name="nounPlural1" id="nounPlural1"><br>
                <li>Liquid: <input type="text" name="liquid" id="liquid"><br>
                <li>Noun (Plural): <input type="text" name="nounPlural" id="nounPlural2"><br>
                <li>Famous Person: <input type="text" name="famousPerson" id="famousPerson"><br>
                <li>Place: <input type="text" name="place" id="place"><br>
                <li>Occupation: <input type="text" name="occupation" id="occupation"><br>
                <li>Noun: <input type="text" name="noun1" id="noun1"><br>
                <li>Nationality: <input type="text" name="nationaltiy" id="nationality"><br>
                <li>Female Celebrity: <input type="text" name="femaleCelebrity" id="femaleCelebrity"><br>
                <li>Noun: <input type="text" name="noun2" id="noun2"><br>
                <li>Female Friend: <input type="text" name="femaleFriend" id="femaleFriend"><br>
                <li>Noun (Plural): <input type="text" name="nounPlural3" id="nounPlural3"><br>
                <li>Number: <input type="text" name="number" id="number"><br>
                <li>Adjective: <input type="text" name="adjective2" id="adjective2"><br>
                <button id="finished">I'M FINISHED!</button>
            </ul>
        </div>
        <div id="story"></div>      
    </body>
</html>

I know that my code practices are probably sloppy--I'm just trying to get it to work before I polish it up. Also, I have to use regular javascript and no jquery. I appreciate any help I get with this because I've been working in vain for hours and hours trying to figure out what's wrong.

Further to @Jaromanda X's suggestion, try wrapping your code in a function, and then assigning that function as the window.onload event handler.

This way your code won't run until the HTML document has been loaded. Otherwise your code might run before the document has loaded and there won't be any HTML elements to operate on.

function runOnLoad() {
    // all your code in here
}

// assign your function as the handler for the onload event
window.onload = runOnLoad;

You might be interested to read this question for a bit more information about how to run code after the page has loaded, and a few different ways to do it.

pure JavaScript equivalent to jQuery's $.ready() how to call a function when the page/dom is ready for it

You have errors in your code. bold() is not a function if you want to use bold text you can use <strong> tag.

Also if you want to get value from textbox you need to use value property.

function createStory() {
   console.log("createStory HAS BEEN CALLED");
   var finalStory = "";
   finalStory += "I enjoy long, <strong>" + adjective1.value+ "</strong>";
   story.innerHTML = finalStory;
   console.log(story + " IS THE STORY");
}

I have corrected for the first textbox you can follow same for the rest.

You could use the addEventListener :

var div = document.getElementById('div');
function blah(e) {

}
div.addEventListener('click', blah);u

This would make a click event in the javascript. The e parameter is needed for this though because it calls for the event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM