Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Client-side Scripting _ Function not carried out?

Posted by: jlittle15 Jan 17 2017, 09:24 PM

Can't seem to get this coding correct. The function never is carried out when the button its pressed. Any help will be appreciated.

<!DOCTYPE html>
<html>
<head>
<script>

function MadLib(){


var a1 = document.AgentID.elements[0].value;
var a2 = document.City.elements[0].value;
var a3 = document.Country.elements[0].value;
var a4 = document.Utensil.elements[0].value;
var a5 = document.Adj1.elements[0].value;
var a6 = document.Animal.elements[0].value;
var a7 = document.Transportation.elements[0].value;


document.write("Agent"+a1+",");
document.write("<br>"+"<br>");
document.write("Congradulations on accepting your next assignment Agent "+a1+"");
document.write("<br>"+"<br>")
document.write("Your flight leaves to "+a2+" , "+a3+" in the next eight hours. You have been granted your weapon of choice, the "+a5+" "+a4+". Your assignment is to capture the "+a6+" with minimal casualties. Your extraction via "+a7+" will be waiting.");
document.write("<br>"+"<br>");
document.write("Best of Luck Agent "+a1+"",);
document.write("<br>");
document.write("Operations HQ");
}

</script>
</head>
<body>

<form name="AgentID">
AgentID
<input type="text">
</form>

<form name="City">
City
<input type="text">
</form>

<form name="Country">
Country
<input type="text">
</form>

<form name="Utensil">
Noun
<input type="text">
</form>

<form name="Adj1">
Adjective
<input type="text">
</form>

<form name="Animal">
Animal
<input type="text">
</form>

<form name="Transportaion">
Transportation
<input type="text">
</form>


<input type="button" value="Accept Your Mission" onClick="MadLib()">

</body>
</html>

Posted by: pandy Jan 18 2017, 05:36 AM

You have one error in the JS. You have a comma outside the quotes here.

CODE
document.write("Best of Luck Agent "+a1+"",);
                                          ^


Not an error, but the quotes at the end here are empty. I guess you meant to have something there.

CODE
document.write("Congradulations on accepting your next assignment Agent "+a1+"");
                                                                             ^^



It still won't run, because you also have a typo on the HTML. "Transportation" will work better. happy.gif

HTML
<form name="Transportaion">

Posted by: pandy Jan 18 2017, 05:39 AM

Also, turn on your browser's error console. It will give you useful, but sometimes cryptic, hints.

Posted by: Christian J Jan 18 2017, 08:04 AM

Also, document.write will overwrite the existing web page. If that's not what you want, change the innerHTML of some HTML element instead.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)