kjarsenal
kjarsenal

Reputation: 934

re to construct javascript array

I have a simple function that checks selected div ids, and performs an action if a particular div is present:

function checkContent_m(){
var mItems = [document.getElementById('m_round1'),document.getElementById('m_round2'),document.getElementById('m_round3'),document.getElementById('m_round4'),
document.getElementById('m_round5'),document.getElementById('m_round6'),document.getElementById('m_round7'),document.getElementById('m_round8')];

if (mItems.length > 0){
        document.getElementById('m_div').style.display = "block";       
}
else{
    document.getElementById('m_div').style.display = "none";
}

}

Seems to me that there might be a way for me to construct my array more efficiently. How would I construct a regular expression that would be an equivalent to:

document.getElementById('m_round'+ '*')

Such statement would allow me to add an unlimited number of "m_round" divs, without having to modify my js function.

Upvotes: 0

Views: 49

Answers (1)

Ryan Berger
Ryan Berger

Reputation: 9732

Consider using jQuery. Then selecting ID's that have similar names is trivial...

$('[id^="m_round"]')

Of course, you could also give all the elements you want to select the same class and then select them that way...

$('.m_round')

Upvotes: 3

Related Questions