adding values to javascript array.

Posted on 2012-08-22
Last Modified: 2012-08-22
I have the following code, looping through form inputs.

Any input whose name has 'bsktMissing' should be added to the array.

I'm specifically calling 'inputs' as an array, instead of object.

		var inputs = [];

			// console.log('key: ' + $(this).attr('name') + '   | Val: ' + $(v).val());
			if( $(this).attr('name').indexOf('Missing') > 1 ){
				inputs[$(this).attr('name').replace('bsktMissing','')] = $(v).val();

Open in new window

If I look at console.log($(this).attr('name').replace('bsktMissing','')) in the loop, the console shows the remaining name string.

If I look at console.log($(v).val()) in the loop, the console shows the input field values.

If I put inputs[ $(this).attr('name').replace('bsktMissing','') ] = $(v).val() in the loop, the inputs array never fills with values.

What am I over looking ?

Question by:Imaginx
    LVL 12

    Accepted Solution

    In Javascript, arrays can only be enumerated.  You can't use them that way.

    You could, however, use an object.
    var inputs = {};
    		// console.log('key: ' + $(this).attr('name') + '   | Val: ' + $(v).val());
    		if( $(this).attr('name').indexOf('Missing') > 1 ){
                            var index = $(this).attr('name').replace('bsktMissing','');
    			inputs[index] = $(v).val();

    Open in new window

    It can be a bit confusing, because you assign the value to the key seemingly the way you would in PHP or other languages.  But it's still an object.

    To further confuse you, you can access the key/value pair in one of two ways: either or inputs['foo'].  

    There's some good information here:
    LVL 1

    Author Comment

    You know ... I made sure to make sure everything was in array format - never thought to just switch it to an object to get the associative feature.

    That worked perfect though, thanks Zppafan2k2.

    Should I be looking out for anything quirky when I pass the jquery object to a php action page, using the $.ajax({}) method ?

    Thanks again.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Adobe Dreamweaver CC 2015

    Adobe Dreamweaver Creative Cloud is used by web designers and front-end developers and allows you to visualize your site in real-time as you code. This course covers exam objectives for the Adobe Certified Associate (ACA) certification.

    Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now