0

I have repeating div's down a page. I need to select the outerbox ID's and text element

<div class="outerbox" id="12345">

        <div class="headerbox" id="title12345">

            <div class="text">&nbsp;</div>
            <h2>Foo Bar
            </h2>
        </div>

I need to create a multi-dimensional array using jquery

I have it this far with creating a single array for the outerbox ID's

var boxids = $('.outerbox').map(function() { return this.id; }).get();
var boxidsLength = boxids.length;

var i = 0; i < boxidsLength; i++) {
    alert (boxids[i])
}

But I'm having difficulty wrapping my head around the process of how 1) I'd create a multi-dimensional array from this input and how to 2) select the element under class 'text'

The array would look like

Foo Bar | Bar Foo

12345   | 67890

etc

Thank you

2
  • If possible , can post literal [[]] of required array ? Not certain about description of result at "The array would look like" Foo Bar | Bar Foo 12345 | 67890 ? [id, text] ? Thanks Commented Sep 28, 2014 at 7:14
  • Sorry, I understand two dimensional arrays as being laid out like a spreadsheet [Foo Bar[1234] Bar Foo [4567]] would be the ideal output so I could call them as array[0][0] and get Foo Bar then array[0][1] to get 1234 Commented Sep 28, 2014 at 7:31

2 Answers 2

1

Edit, Updated

Try

var boxids = $('.outerbox').map(function() { 
  return [[$.trim($(".text + *", this)[0].innerText), this.id]] ; 
}).get();

var boxids = $('.outerbox').map(function() { 
  return [[$.trim($(".text + *", this)[0].innerText), this.id]] ; 
}).get();
$("body").text(boxids);
console.log(boxids);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="outerbox" id="12345">

        <div class="headerbox" id="title12345">

            <div class="text">&nbsp;</div>
            <h2>Foo Bar
            </h2>
        </div>
    
    <div class="outerbox" id="67890">

        <div class="headerbox" id="title678905">

            <div class="text">&nbsp;</div>
            <h2>bar foo
            </h2>
        </div>

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

3 Comments

What am I missing when I have this box repeating? See jsfiddle.net/z183gfpx
Not certain what mean by "missing" ? console.log(boxids[0][0], boxids[0][1], boxids[1][0], boxids[1][1]); ?
If there are two instances of the 'outerbox' code above, one called foo bar and one called bar foo, with different IDs I get [foo bar bar foo[12345][foo bar bar foo[67890]]
0

I think this should work:

var array = [];
$('.outerbox').each(function ()
{
    var id = this.id;
    var text = $(this).find('.text').next().text();

    array[text] = id;
});

But maybe you need to sanitize the text, to trim spaces and line breaks.

Comments

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.