0

I do have many DIVs with the same class .list. They are filled with other divs with the ID element. In these element divs there a checkboxes and textfields. I'm able to get all the checked checkboxes' next Textfield's value. I've saved them into one Array but I want an Array for each .list.

That's how my Code looks so far:

function test(){
 var array = new Array();
    $(".list > #element").each(function(){
    array.push($(this).find('input:checkbox[class=unterpunkte]:checked').next('input[type=textfield]').val());
    });
    console.log(array);
}

How can I dynamcially creat an Array for each list. I don't want to have one Array where all the Values are saved. I need Arrays for each div with class .list. Any ideas?

HTML/PHP:

echo("<div class='list' name='$oberpname' value='$oberpname'>");

        while($satz != null)
        {
            echo("<div id='element'><label><input type='checkbox' class='unterpunkte' name='$satz[unterpname]' value='$satz[unterpid]'><input type='textfield' class='$oberpname' value='$satz[unterpname]' readonly/></label></div>");
            $satz=mysql_fetch_assoc($cursor);
        }

        echo("</div>"); //.list div end

EDIT NOTE: Added HTML/PHP

9
  • please provide the html Commented Feb 27, 2014 at 7:59
  • 2
    IDs must be unique, by definition. Commented Feb 27, 2014 at 8:02
  • @Preprocezzor:you should provide rendered html Commented Feb 27, 2014 at 8:06
  • Can you explain me the difference between a rendered html and my html? Commented Feb 27, 2014 at 8:08
  • textfield is not a valid input type. Commented Feb 27, 2014 at 8:11

1 Answer 1

4

Note: ID of an element must be unique, so instead of using element as an ID use it as a class.

You can create an array of arrays, and access the desired list of checkboxes using the index of the list element lik

function test() {
    var array = new Array();
    $(".list").each(function () {
        var vals = $(this).find('input:checkbox[class=unterpunkte]:checked').next('input[type=textfield]').map(function () {
            return this.value;
        }).get();
        array.push(vals)
    })
    console.log(array);
}

Now array[0] will give the values for list 1 where as array[1] will give the values for list 2

Sign up to request clarification or add additional context in comments.

3 Comments

This worked for me. Thank you. I have a question about my 'not-rendered' HTML. Is there a better way to make it in a rendered way? I used PHP because I got some values from database and filled them in PHP Varaibles.
your PHP code is ok... but when you post a question related to html we will be more interested in how the actual html might look like than the template because if you share the PHP or any other template then we need to spent time on going through that to findout what will be the actual html
The best way is to in the browser go to view source on the target page and get the html from there - that will be the actual html created by your php code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.