views:

58

answers:

1

Hello,

I am trying to create a jQuery script that would remove a div/s based on what the class of the parent div is, for example based on the parent div class below i.e one-column I would like remove divs sideColumn-two & sideColumn-three

<div id="footer" class="one-column">
    <div class="wrapper">
        <div class="contentColumn">
            Main Content Column
        </div>
    </div>

    <div class="sideColumn-one">
        Side Column 1
    </div>

    <div class="sideColumn-two">
        Side Column 2
    </div>

    <div class="sideColumn-three">
        Side Column 2
    </div>       
</div>

And if the parent's div class is two-column then it would remove divs sideColumn-one and sideColumn-three, and so forth.

Additional Info - The parent divs are called as follows:

one-column
one-column-three
one-column-two-one
two-columns-left-sidebar
two-columns-right-sidebar
two-columns-equal
two-columns-equal-right
two-columns-50
two-columns-stacked-left
two-columns-stacked-right
two-columns-mixed
two-columns-mixed-left
two-columns-mixed-content-left
three-columns-mid
three-columns-equal
three-columns-sidebars-right
three-columns-sidebars-left
four-columns-equal
four-columns-mag

The div contentColumn is never removed

Any help would be greatly appreciated.

Thanks & Regards

Said

+3  A: 

Edit: For updated markup

var num = $("#footer").attr("class").replace("-column","");
$("#footer > div:gt(0):not(.sideColumn-" + num + ")").hide();​

You can try it here


Previous answer: Without changing your markup, you could so something like this:

var nums = { one: 1, two: 2, three: 3 };
var num = $("#test").attr("class")​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​.replace("-column","");
$("#test > div:not(.col" + nums[num] + ")").hide();​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

You can give it a try here, though if you simplfied it by giving the parent the same class for example:

<div id="test" class="col1">
    <div class="col1">
        Col 1
    </div>
    <div class="col2">
        Col 2
    </div>    
    <div class="col3">
        Col 3
    </div> 
</div>​

The script gets simpler, like this:

$("#test > div:not(." + $("#test").attr("class") + ")").hide();​

You can try that version here, if you want to remove instead of hide them, just replace .hide() with .remove().

Nick Craver
Unfortunately I can't have the parent the same class as it determines how the layout is going to be, i.e there can be cases where I would show two columns and remove only one
By the way, Thanks Nick, I am testing it now with the actuall code
Nick - thanks for the input, very appreciated, however, based on the actual html I have, the script is will not work, my mistake for not including it here, but have done so now and my apologies.
@saidsl - Based on your markup, try this: http://jsfiddle.net/nick_craver/XpdsC/2/
Nick Craver
@Nick - thanks Nick, getting there, just noticed that I should have said if its a two column than it would would remove column three. basically what I need to achieve is something like this
@saidsl - I'm sorry...I'm not following you at all, this is much easier done server-side anyway, but I don't know what you're after, since the question keeps changing... At this point it sounds like you need a full object/selector map, e.g. if this remove this, because based on the new information each time, I don't see a simple way to do what you're trying.
Nick Craver
@Nick - my apologies for the changes. But you are correct in your comment about "if this remove this" which is what I am trying to achieve. Basically, the HTML structure you see above allows me to have 19 different layouts without changing it in anyway, all changes are done via css, what I am trying to achieve is to remove the divs I would normally hide via CSS. Hope this provides some clarification
@saidsl - Why not hide them in CSS as you're already doing, keep your layout in one place, then just do `$("#footer > :hidden").remove()` ?
Nick Craver
@Nick - awesome, simplest and best solution. Thanks Heaps Nick, very appreciated
@saidsl - welcome :) sorry it took a bit for me to gather how your setup is
Nick Craver
@Nick - no probs, it was largely my fault, should better articulate requirements next time