Pete Norris
Pete Norris

Reputation: 1054

jquery add class if blank attribute

I have a div with a title attribute:

<div id="video" title="<?php echo $row_rs_dealItem['video']; ?>">

Basically if the returned value from mysql is blank then I want to add a class. I have the following script:

$(document).ready(function(e) {
    $('#video[title*=""]').addClass('invisible');
});

It seems really easy, so don't know what I'm doing wrong. THanks guys

Upvotes: 0

Views: 1724

Answers (4)

Prasenjit Kumar Nag
Prasenjit Kumar Nag

Reputation: 13461

Why dont you do this instead from php

echo '<div id="video" title="'. $row_rs_dealItem['video'] .'" class ="'. ((!$row_rs_dealItem['video'])?"invisible":"").'">';

Upvotes: 0

optimusprime619
optimusprime619

Reputation: 764

haven't done this myself but suggest trying

if($("#video").attr("title") == "") {
 $("#video").addClass("invisible");
}

Upvotes: 0

Dunhamzzz
Dunhamzzz

Reputation: 14808

Why do this in javascript?

I assume no title = not wanting to display the video?

if( !empty($row_rs_dealItem['video']) ) {
    echo '<div id="video" title="'. $row_rs_dealItem['video'] .'">';
}

As pointed out you could use this to add the class if you still want users to load the unused markup...

Upvotes: 1

Ry-
Ry-

Reputation: 225044

*= means "contains". All of your title attributes will contain the empty string. Just change it to =:

$('#video[title=""]').addClass('invisible');

Upvotes: 7

Related Questions