I want to use jquery to:
- Get user input several times and append the input to a div.
- Store each of the user's inputs in a single array so that I can save them.
- Be able to pass the input value (at a specific index in the array) to a javascript function (search) when the user clicks on the input within the div it was appended to.
Here is my code so far:
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type = "text/javascript" src='script.js'>
</script>
<title></title>
</head>
<body>
<form name="searchForm">
<input type="text" id="search" placeholder="Enter Card Name" name="searchItem"/>
</form>
<div id="button">Search!</div>
<br/>
<div id="output"></div>
<div id="save"></div>
</body>
</html>
Javascript:
function Vanguard(name,grade,skill,power,shield,critical, type, nation, clan, race){
this.name = name;
this.grade = grade;
this.skill = skill;
this.power = power;
this.shield = shield;
this.critical = critical;
this.type = type;
this.nation = nation;
this.clan = clan;
this.race = race;
};
var asurakaiser = new Vanguard("Asura Kaiser", 3, "Twin Drive!!", 11000, 0, 1, "Normal Unit", "Star Gate", "Nova Grappler", "Battleroid");
var kingofknightsalfred = new Vanguard("King of Knights, Alfred", 3, "Twin Drive!!", 10000, 0, 1, "Normal Unit", "United Sanctuary", "Royal Paladin", "Human");
var units = new Array(asurakaiser, kingofknightsalfred);
function printVanguard(p){
document.getElementById('output').innerHTML +=("<hr />");
document.getElementById('output').innerHTML +=("Name: " + p.name);
document.getElementById('output').innerHTML +=("<br />Grade: " + p.grade);
document.getElementById('output').innerHTML +=("<br />Skill: " + p.skill);
document.getElementById('output').innerHTML +=("<br />Power: " + p.power);
document.getElementById('output').innerHTML +=("<br />Shield: " + p.shield);
document.getElementById('output').innerHTML +=("<br />Critical: " + p.critical);
document.getElementById('output').innerHTML +=("<br />Type: " + p.type);
document.getElementById('output').innerHTML +=("<br />Nation: " + p.nation);
document.getElementById('output').innerHTML +=("<br />Clan: " + p.clan);
document.getElementById('output').innerHTML +=("<br />Race: " + p.race);
document.getElementById('output').innerHTML +=("<br />");
};
var search = function(name){
for (i = 0; i < units.length; i++){
if (units[i].name === name) {
printVanguard(units[i]);
}
}
};
$(document).ready(function() {
$('#button').click(function(){
var output = $('input[name=searchItem]').val();
$('#output').append("<br />" + '<div class="item2">' + output + '</div>');
$('#output').click(function(){
$('#save').append(search(output));
});
});
});
So basically look at the jquery part... if you notice, when the user clicks on the button, it will save the input to the variable "output" (I dunno why I chose that name because looking back it sounds confusing). But anyway, in the line after, the variable output will append to the div output. And if the user clicks on the output itself within the div, it will append a function (search) which takes the user's input as the parameter.
But something is wrong with my code... because if I enter "Asura Kaiser" without the quotes, click on the name, it will show all the correct data based on the search function which calls search("Asura Kaiser"). But then when I click on this result, it displays the same data yet again. If I try to enter a different name like "King of Knights, Alfred" without the quotes, it still displays the results for "Asura Kaiser" in addition to the results for "King of Knights, Alfred".
Basically, what seems to be happening is my output variable is accumulating data or something.
I think that, if I stored each of the user's inputs in a unique element of an array, and call on that specific element when I put it in the parameter for the search function, I should be able to get my program to work correctly.
Any help would be appreciated. To clarify, what I'm looking for is a way to store very many user input's and to be able to pass them as unique parameters to the search function based on their index in the array.
Something like search(output[i]) for some index within the array.
The overall goal of this program is so that people can search for card information for a popular card game called Cardfight!! Vanguard. I basically am aiming to create a program that will allow you to look up cards and make a deck, and this is the start of that long process.
Thanks for reading!