I want to be able to click the submit
button after inputting some text into the forms, then click the results
button to display what I just input.
It seems like the titles
variable is "stuck" in the function onClick(event)
function. How do I "get it out?"
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<body>
<script type="text/javascript">
$(document).ready(function() {
$(function() {
$('#submit').click(onClick);
});
function onClick(event) {
var titles = $('input[name^=titles]').map(function(idx, elem) {
return $(elem).val();
}).get();
window.alert(titles); //it displays the titles here
}
$("#result").click(function() {
window.alert('x');
window.alert(titles); //but not here
$("p").text(titles[0]);
});
});
</script>
<p>display results here</p>
<input name="titles[]">
<input name="titles[]">
<button id="submit">submit</button>
<button id="results">results</button>
</body>
This has probably been answered already but I don't know enough jargon to effectively search for an answer. Also I used some of the code from here cause I want to take in two string inputs: Get values from multiple inputs jQuery