Jim
Jim

Reputation: 923

jQuery - hide elements after specific text

i want to hide some elements after a specific text / the last word of the first div

<div class="top">here is some text</div>
<table class="middle">
...
</table>
<div class="bottom">...</div>

hide table(middle) and the div(bottom) depending on the word "text" from the first div

Upvotes: 0

Views: 601

Answers (6)

billyonecan
billyonecan

Reputation: 20260

You can get the last word using: $('.top').text().split(' ').pop(), and then add some simple logic to show/hide the other elements:

var lastWord = $('.top').text().split(' ').pop();
$('.middle, .bottom').toggle(lastWord == 'text');

Upvotes: 1

ShibinRagh
ShibinRagh

Reputation: 6646

try

http://jsfiddle.net/EnQym/1/

txtWord = $('.top').text().split('text')[1]
if(txtWord){
alert("div show");
}else{

alert("div hide");
}

Upvotes: 1

Muhammad Bekette
Muhammad Bekette

Reputation: 1434

lets have a method you call

hideOrShow(){
if(document.getElementsByClassName('top')[0].innerHtml=="the text you want"){
document.getElementsByClassName('middle')[0].style.display='none';
document.getElementsByClassName('bottom')[0].style.display='none';
}else{
document.getElementsByClassName('middle')[0].style.display='block';
document.getElementsByClassName('bottom')[0].style.display='block';
}
}

Upvotes: 0

A. Wolff
A. Wolff

Reputation: 74420

var lastWord = function (o) {
    return ("" + o).replace(/[\s-]+$/, '').split(/[\s-]/).pop();
};

if (lastWord($('.top').html()) === "mykeyword") {
    $('.middle,.bottom').hide();
} else $('.middle,.bottom').show();

Upvotes: 0

Emanuele Pavanello
Emanuele Pavanello

Reputation: 863

If you need to check the text of the first div and show the relative div



    if($(".top").html() == "what you want")
    {
        $(".middle").show();
        $(".bottom").hide();
    }
    else
    {
        $(".bottom").show();
        $(".middle").hide();
    }

Upvotes: 0

edonbajrami
edonbajrami

Reputation: 2206

Example if in your .top div text is hide or show the function must be:

var txt = $('.top').text();

if(txt=='hide')
{
   $('.middle').hide();
   $('.bottom').hide();
}
else if(txt=='show')
{
   $('.middle').show();
   $('.bottom').show();
}

Upvotes: 0

Related Questions