Thread: JQuery Arrays

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    JQuery Arrays


    I have created a script with a single input box. What I would like is for the user to enter text, hit enter and have that text added to an array. Then the text in the input box is removed and the user can submit other text. They can repeat this process as often as they want and the array will just keep growing.

    I defined an array: var newarray = [];

    I have function that listens for key presses and when the ENTER key is hit, it performs some logic. Everything works fine except for this:

    Code:
    if ($("#Commands").text() == "SCAN UPC"){
    	newarray.push($('input[id=Input]').val());
    	$('input[id=Input]').val('');
    	return false;
    }
    What I thought it would do is take the text entered in the Commands input box and push it to the newarray array. If I take out the first line "newarray.push($('input[id=Input]').val());" it runs fine, removing the text everytime I hit ENTER. With that line in, the text just stays in the box.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,187
    Rep Power
    184
    Try this:
    Code:
    if ($("#Commands").val().length >= 1){
    	newarray.push($(this).val());
    	$(this).val('');
    	return false;
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by web_loone08
    Try this:
    Code:
    if ($("#Commands").val().length >= 1){
    	newarray.push($(this).val());
    	$(this).val('');
    	return false;
    }
    I did that. It still seems to die at the newarray.push($(this).val() line.

    If I comment that out, the script runs fine. I can enter text, press enter and the text disappears allowing me to enter additional text. However it doesn't populate the array.

    With that line uncommitted, the script just stops working. I enter text, press enter and nothing happens.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    My mistake. I declared my array inside of
    Code:
    jQuery(document).ready(function () {
    	var newarray = [];
    });
    I moved it outside of that and it works fine now. Thanks for you help.

IMN logo majestic logo threadwatch logo seochat tools logo