Reputation: 1
I'm trying to make a javascript collect data from user input, specifically textboxes and dropdown options, and produce user output. Any help would be greatly appreciated.
The javascript is supposed to collect the values in the first textbox, convert it to lowercase, and then see if it is a correct answer. If it is, it should then check if the user wants additional information on the movies they have acted in ("Yes"), or not ("No"). The actor's code in the database is meant to be displayed in the first paragraph, whilst the list of movies the actor has been in should be displayed in the second paragraph. With each movie being displayed on a separate line.
function gather()
{
var name = document.getElementById("name").value;
return name.toLowerCase();
var info = document.getElementById("info").value;
return info.toLowerCase();
var code = document.getElementById("code").value;
var titles = document.getElementById("titles").value;
if (name = "patrick swayzee")
{
code = "Patrick Swayzee's code is ps01";
if (info = "yes")
{
titles = "Film 1" + \n + "Film 2";
}
else
{
titles = "";
}
}
else
{
code = "Request does not exist. Please enter another actor/actress's name."
}
}
<html>
<head>
<title>Actor/Actress Database</title>
<script src="act_req.js" type="text/javascript">
</script>
</head>
<body>
<table>
<tr>
<td><b>Name:</b></td>
<td><b>Info:</b></td>
<td></td>
</tr><tr>
<td><input type="text" id="name="></input></td>
<td>
<select id="info">
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
</td>
<td><button onclick="gather()">Search</button></td>
</tr>
</table>
<p id="code"></p>
<p id="titles"></p>
</body>
</html>
Upvotes: 0
Views: 48
Reputation: 177975
<button type="button"
You may mean
window.onload=function() {
document.getElementById("gather").onclick=function() {
var name = document.getElementById("name").value.toLowerCase();
var info = document.getElementById("info").value.toLowerCase();
var code = document.getElementById("code");
var titles = document.getElementById("titles");
var codeString = "", titleString="";
if (name == "patrick swayzee") {
codeString = "Patrick Swayzee's code is ps01";
titleString = (info == "yes")?"Film 1\nFilm 2":"";
} else {
codeString = "Request does not exist. Please enter another actor/actress's name."
}
code.innerHTML=codeString;
titles.innerHTML=titleString;
}
}
<table>
<tr>
<td><b>Name:</b></td>
<td><b>Info:</b></td>
<td></td>
</tr>
<tr>
<td>
<input type="text" id="name" value=""/></td>
<td>
<select id="info">
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
</td>
<td>
<button type="button" id="gather" >Search</button>
</td>
</tr>
</table>
<p id="code"></p>
<p id="titles"></p>
Upvotes: 0
Reputation: 63524
Right here's a full run down of all the things I found in your code that would stop it working (because I'm a little bored):
1)
return name.toLowerCase();
return info.toLowerCase();
This is not how return
works. If you want to put the values into lowercase you can do:
var name = document.getElementById("name").value.toLowerCase();
2)
var code = document.getElementById("code").value;
Here you're trying to assign the the value of "code" to a variable. The problem with this is that you try to set the value of that variable later on in the code. What you meant to do was just capture the element itself in that variable:
var code = document.getElementById("code");
That's the same for the var titles
line btw.
3)
if (name = "patrick swayzee")
Here you're assigning name a value. You want to use ===
(strict equality is best).
Same for the if (info
line too
4)
code = "Patrick Swayzee's code is ps01";
Where you are trying to assign something to our elements (see 2), you should now assign it to a property of that element. IE9+ and recent browsers use textContent
, so this line should read:
code.textContent = "Patrick Swayzee's code is ps01";
Same in all the other places that's occurring.
5)
<td><input type="text" id="name=">
should be
<td><input type="text" id="name">
Notice the difference? One typo can break your code.
There's probably one or two things I've missed, but here's some working code - look it over. The other answerers will probably have caught the rest. Best of luck with your programming.
Upvotes: 1
Reputation: 1176
you're missing an extra = in the conditional:
if (info == "yes")
in fact, you should use === so jshint won't complain about it.
Upvotes: 1