Javascript help using helper function random button to display 1 of 5 different input boxes? -
i create 1 set of 5 input boxes using submit button produce results producing random mad lib. 1.)create submit button when clicked randomly chooses number runs mad lib 2.)without changing page creates boxes according mad lib 3.) gets user input 4.)prints out mad lib
.is
document.addeventlistener("domcontentloaded",init); function init(){ var randombutton = document.createelement('button'); randombutton.innerhtml ="random"; randombutton.addeventlistener("click", random); document.getelementbyid("displayarea"); } function random(){ var randomnumber = math.random(); randomnumber *= 10; randomnumber = math.floor(randomnumber); randomnumber = randomnumber % 7; console.log(randomnumber); if(randomnumber==1){ var userdataobject = buildingmadlib0(); var madlib0 = madlib0(userdataobject); var displayarea = document.getelementbyid("madlib0"); displayarea.innerhtml = madlid0; }else if(randomnumber==2){ var userdataobject = buildingmadlib1(); var madlib1 = madlib1(userdataobject); var displayarea = document.getelementbyid("madlib1"); displayarea.innerhtml = madlid1; }else if(randomnumber==3){ var userdataobject = buildingmadlib2(); var madlib2 = madlib2(userdataobject); var displayarea = document.getelementbyid("madlib2"); displayarea.innerhtml = madlid2; }else if(randomnumber==4){ var userdataobject = buildingmadlib3(); var madlib3 = madlib1(userdataobject); var displayarea = document.getelementbyid("madlib3"); displayarea.innerhtml = madlid3; }else if(randomnumber==5){ var userdataobject = buildingmadlib4(); var madlib4 = madlib4(userdataobject); var displayarea = document.getelementbyid("madlib4"); displayarea.innerhtml = madlid4; } } //sfunction formsubmitted(){ //} function buildingmadlib0(){ var noun = document.getelementbyid("noun").value; checkinputwithwarning(noun,"noun"); var noun = document.getelementbyid("noun1").value; checkinputwithwarning(noun1,"noun"); var noun = document.getelementbyid("noun2").value; checkinputwithwarning(noun2,"noun"); var place = document.getelementbyid("place").value; checkinputwithwarning(place,"place"); var adjective = document.getelementbyid("adjective").value; checkinputwithwarning(adjective,"adjective"); var noun = document.getelementbyid("noun3").value; checkinputwithwarning(noun3,"noun"); var madliboneobjects = {noun:noun, noun1:noun1, noun2:noun2, place:place, adjective:adjective, noun3:noun3}; return madliboneobjects; } function buildingmadlib1(){ var adjective = document.getelementbyid("adjective").value; checkinputwithwarning(adjective,"adjective"); var noun = document.getelementbyid("noun").value; checkinputwithwarning(noun,"noun"); var animal = document.getelementbyid("animal").value; checkinputwithwarning(aniaml,"animal"); var noise = document.getelementbyid("noise").value; checkinputwithwarning(noise,"noise"); var madlibtwoobjects = { adjective:adjective, noun:noun, animal:animal, noise:noise,}; return madlibtwoobjects; } function buildingmadlib2(){ var noun = document.getelementbyid("noun").value; checkinputwithwarning(noun,"noun"); var verb = document.getelementbyid("verb").value; checkinputwithwarning(verb,"verb"); var noun = document.getelementbyid("noun1").value; checkinputwithwarning(noun1,"noun"); var adjective = document.getelementbyid("adjective").value; checkinputwithwarning(adjective,"adjective"); var madlibthreeobjects = { noun:noun, verb:verb, noun1:noun1, adjective:adjective}; return madlibthreeojects; } function buildingmadlib3(){ var noun = document.getelementbyid("noun1").value; checkinputwithwarning(noun1,"noun"); var verb = document.getelementbyid("verb").value; checkinputwithwarning(verb,"verb"); var noun = document.getelementbyid("noun2").value; checkinputwithwarning(noun2,"noun"); var pronoun = document.getelementbyid("pronoun").value; checkinputwithwarning(pronoun,"pronoun"); var noun = document.getelementbyid("noun3").value; checkinputwithwarning(noun3,"noun"); var pronoun = document.getelementbyid("pronoun1").value; checkinputwithwarning(pronoun1,"pronoun"); var verb = document.getelementbyid("verb2").value; checkinputwithwarning(verb2,"verb"); var madlibfourobjects = {noun1:noun1, verb:verb, noun2:noun2, pronoun:pronoun, noun3:noun3, pronoun1:pronoun1, verb2:verb2}; return madlibfourobjects; } function buildingmadlib4(){ var verb = document.getelementbyid("verb").value; checkinputwithwarning(verb,"verb"); var noun = document.getelementbyid("noun").value; checkinputwithwarning(noun,"noun"); var noun = document.getelementbyid("noun1").value; checkinputwithwarning(noun1,"noun"); var noun = document.getelementbyid("noun2").value; checkinputwithwarning(noun2,"noun"); var noun = document.getelementbyid("noun3").value; checkinputwithwarning(noun3,"noun"); var madlibfiveobjects = {verb:verb, noun:noun, noun1:noun1, noun2:noun2, noun3:noun3}; return madlibfiveobjects; } function madlib0(userdataobject){ var madlib0 = "be kind "+ userdataobject.noun + "footed "+ userdataobject.noun ; madlib0 += "for duck may somebody`s" +userdataobject.noun + ", "; madlib0 += "be kind in " + userdataobject.place; madlib0 += "where weather " + userdataobject.adjective; madlib0 += "you may think the" + userdataobject.noun + " ,"; madlib0 += "well is."; } function madlib1(userdataobject){ var madlib1 = userdataobject.adjective + "macdonald had "+ userdataobject.noun + ", e-i-e-i-o"; madlib1 += "and on had "+ userdataobject.animal +",e-i-e-i o"; madlib1 += "with a" + userdataobject.noise +" here"; madlib1 += "and there,"; madlib1 += "here a, there a,"; madlib1 += "everywhere a,"; madlib1 += "macdonald had a, e-i-e-i-o"; } function madlib2(userdataobject){ var mablib2 = "there many "+ userdataobject.noun + userdataobject.verb; madlib2 += userdataobject.noun +"on " + userdataobject.adjective; madlib2 += "plane! screamed."; } function madlib3(userdataobject){ var madlib3 = "one day " +userdataobject.noun; madlib3 += "was "+ userdataobject.verb +" the"; madlib3 += userdataobject.noun + " on way."; madlib3 += userdataobject.pronoun +" saw " + userdataobject.noun; madlib3 += "this suprise"; madlid3 += "so "+ userdataobject.pronoun + userdataobject.verb +" quickly."; } function madlib4(userdataobject){ var madlib4 = "mothers sit around "+userdataobject.verb+", last"; madlib4 += "summer, little brother fell " +userdataobject.noun+" , got"; madlib4 += "poisen "+ userdataobject.noun +" on " + userdataobject.noun; madlib4 += "my family going "+ userdataobject.noun + "."; } <html> <title>madlib</title> <script src="madlib.js"></script> </head> <body> <p><div>click submit randomly mad lib!!!</div></p> <p><button id="random">submit</button></p> <div id="displayarea"></div> <fieldset id="madlib0"> noun: <input id="noun" type="text"> noun: <input id="noun1" type="text"> noun: <input id="noun2" type="text"> place: <input id="place" type="text"> adjective: <input id="adjective" type="text"> noun: <input id="noun3" type="text"> <button id="submit">submit</button> </fieldset> <fieldset id="madlib1"> adjective:<input id="adjective" type="text"> noun:<input id="noun" type="text"> animal:<input id="animal" type="text"> noise:<input id="noise" type="text"> <button id="submit">submit</button> </fieldset> <fieldset id="madlib2"> noun:<input id="noun" type="text"> verb:<input id="verb" type="text"> noun:<input id="noun" type="text"> adjective:<input id="adjective" type="text"> <button id="submit">submit</button> </fieldset> <fieldset id="madlib3"> noun:<input id="noun1" type="text"> verb:<input id="verb" type="text"> noun:<input id="noun2" type="text"> pronoun:<input id="pronoun" type="text"> noun:<input id="noun3" type="text"> pronoun:<input id="pronoun1" type="text"> verb:<input id="verb2" type="text"> <button id="submit">submit</button> </fieldset> <fieldset id="madlib4"> verb:<input id="verb" type="text"> noun:<input id="noun" type="text"> noun:<input id="noun1" type="text"> noun:<input id="noun2" type="text"> noun:<input id="noun3" type="text"> <button id="submit">submit</button> </fieldset> </body> </html>
the second line of html body
<p><button id="random">submit</button></p>
if that's button want use invoke random()
function, should change line assign var randombutton
this:
var randombutton = document.getelementbyid('random');
Comments
Post a Comment