Ask coding questions

← Back to all posts
MadLibs Story Generator Coding Help
EliseKon (2)
Basically, i need help with having the user typed words in the text boxes to appear in the story

Answered by heyitsmarcus (290) [earned 5 cycles]
View Answer
heyitsmarcus (290)

Alright Elise,

There were a few things that weren't quite right here, but you were so close!

Errors in index.html
When you add any attribute to an HTML element, be sure to wrap them in quotes (these can be double or single quotes). But, they must be wrapped in quotes. And also in the repl (this may not be a problem for the student submission) but there was no reference to script.js so the JavaScript would never run.

Errors in script.js
You have a couple rogue var in there that aren't doing anything except causing the JavaScript to crash. I just erased those from the script. And the only other thing is that you need to get the value from each input. So, when you make:

var noun = document.getElementById("noun");

noun is just a reference to the input element. And to get the value out of any input, you get its value property. So, in the storyDiv.innerHTML just add a .value to each input reference.

storyDiv.innerHTML = "The weekend started when the " + noun.value + " caught on fire. Someone "+ adjective.value + " grabbed water from the nearest "+ noun2.value + ". They quickly started throwing the water onto the flames. After the fire was put out the "+ adjective2.value + " person " + verb.value + " away. The story was in the next days' paper.";

Here's the repl I made with your code:

If this solved your problem, please consider upvoting and marking my answer as the accepted answer to close this question. But, I'll continue answering any questions you may have.

EliseKon (2)

@heyitsmarcus Thank you so much! This is the second time you've saved me. Thanks to you I'll be graduating this year. I couldn't figure this out and was worried that I might not pass the class.

heyitsmarcus (290)

@EliseKon you were so close! Just a few little errors here and there and it was all corrected! Remember, you did all the leg work, all I did was come in and fix a couple things! :) Happy coding, Elise, and good luck!

heyitsmarcus (290)

Hey, Elise,

Please post your code into a repl because we can't see student submissions.