Wednesday, 15 August 2012

javascript - jQuery - Look for a specific word -



javascript - jQuery - Look for a specific word -

i trying display different content on wp post depending on metabox (dropdown)'s value. if had "video", "code" , "image" available options in dropdown, though of making hidden div "video", "code" or "image" depending on selected one. need jquery (or javascript) @ hidden div ether word, , show/hide div's.

here code have far.

if (jquery("div.type:contains('video')")) { jquery(".code").hide(); jquery(".image").hide(); }

but need hide div's except right one.

example

html

<div class="type">video</div> <div class="video">video here</div> <div class="code">code here</div> <div class="image">image here</div>

jquery

if (jquery("div.type:contains('video')")) { jquery(".video").show(); jquery(".code").hide(); jquery(".image").hide(); } if (jquery("div.type:contains('code')")) { jquery(".video").hide(); jquery(".code").show(); jquery(".image").hide(); } if (jquery("div.type:contains('image')")) { jquery(".video").hide(); jquery(".code").hide(); jquery(".image").show(); }

why not working?

look @ jsfiddle whatever entered <div class="type"> show <div class="image">image here</div>

jsfiddle link here

you'll want check returned jquery object it's length:

if (jquery("div.type:contains('video')").length) { jquery(".video").show(); jquery(".code").hide(); jquery(".image").hide(); }

see updated fiddle...

to go bit more detail, problem of if statements returning true because jquery selectors homecoming jquery objects, whether empty or not. if statement evaluating true because asking if jquery object not undefined or false. jquery object neither of these things. remember check length of jquery object when trying determine if found.

update

in terms of showing nil when there nil in div.type, hide before if statements evaluate. this...

javascript jquery html css contains

No comments:

Post a Comment