1

Trying to push the value I get from the onlick to a empty array and I cant seem to get it right.

var result = [];

$('#submitButton').click(function(){

var textField = $('#textField').val();

$('#result').append(textField, '<br/>'); 

$('#textField').val('');
 result($(this).val());

});

Carter
  • 113
  • 1
  • 1
  • 9

3 Answers3

0

Use this to push values in array

result.push($(this).val())
Kamran Jabbar
  • 858
  • 7
  • 21
0
var result = [];

$('#submitButton').click(function(){

var textField = $('#textField').val();

$('#result').append(textField, '<br/>'); 

$('#textField').val('');
 result.push($(this).val()); /* You have to use push method */
});
Kashyap
  • 381
  • 4
  • 10
0

In your code, this refers to button(#submitButton), not to element of which you want to access value.

Use Array.prototype.push to push value in array

var result = [];
$('#submitButton').click(function() {
  var textField = $('#textField').val();
  $('#result').append(textField, '<br/>');
  $('#textField').val('');
  result.push(textField);
});
Rayon
  • 36,219
  • 4
  • 49
  • 76