SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast pyro.699's Avatar
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [jQuery] Creating array from selectors

    So, i have this section of html:

    HTML Code:
    <input type="text" id="inputA_1" value="ValueA" />
    <input type="text" id="inputB_1" value="ValueB" />
    <input type="text" id="inputC_1" value="ValueC" />
    <input type="text" id="inputD_1" value="ValueD" />
    And i want to build this basic java script variable
    HTML Code:
    var postdata = {inputA: 'ValueA',
        inputB: 'ValueB',
        inputC: 'ValueC',
        inputD: 'ValueD'}
    In reality there are approximately 100 variables. (Its a template builder).

    Currently im doing it like this
    HTML Code:
    function buildPostData(id){
        var postdata = {inputA: $("#inputA_"+id).val(), inputB: $("#inputB_"+id).val(), inputC: $("#inputC_"+id).val(), inputD: $("#inputD_"+id).val()};
    }
    Now, i KNOW there is a better way to do this. Im just not 100% sure on how to use the array.push function properly.

    Here is what i have so far:
    HTML Code:
    function buildPostData(id){
        var postdata = [];
        $("input[id$='_"+id+"']").each(function(){
            var id = $(this).attr("id");
            var value = $(this).val();
            
            //Now how to i push it into the array properly?
            postdata.push(id: value);
        });
    }
    Thanks alot
    ~Cody Woolaver

  2. #2
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your best bet would to be wrap the input in a named element (div, ul) and then check from there.

    The following scrip gets all the inputs and makes an key => value array (what does JavaScript call it? Object notation or something. Two many names for the same thing! I call em associative arrays or dictionaries).

    HTML Code:
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    	var postData = {};
    	$("#inputs input").each(function() {
    		postData[this.id] = $(this).val();
    	});
    });
    </script>
    <div id="inputs">
    	<input type="text" id="inputA_1" value="ValueA" />
    	<input type="text" id="inputB_1" value="ValueB" />
    	<input type="text" id="inputC_1" value="ValueC" />
    	<input type="text" id="inputD_1" value="ValueD" />
    </div>

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You would need to push an array, onto that outer array.
    Code:
    postdata.push([id, value)];
    But, you could also
    Code:
    // use object as assoc array/hash map
    var postdata = {};
    $("input[id$='_"+id+"']").each(function(){
        postData[this.id] = this.value;
    });

  4. #4
    SitePoint Enthusiast pyro.699's Avatar
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou both I got it


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •