Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery If is set, or if is not null?

I have the following jQuery Code:

$("#menu span").click(function() {
    var url = this.getAttribute("data-url");
    var mobile = this.getAttribute("data-mobile");
    var facebook = this.getAttribute("data-facebook");

    if (url) {

    }

    if (mobile) {

    }

    if (facebook) {

    }

};

But it is a little glitchy. Is there an alternative I can use to see if this data attribute exists? Instead of using if (url) { }

My HTML will be something like this:

<ul>
    <li><span data-mobile="1" data-url="http://url.com">Site #1</span></li>
    <li><span data-url="http://site2.com" data-facebook="http://fblink">Site #2</span></li>
    <li><span data-url="http://site3.com">Site #3</span></li>
</ul>

So not everyone will have all of the data attributes.


Edit: How do I clear everything once the span is clicked again?

For example:

if (data.mobile) {

    $(".mobile").attr("data-link", data.mobile);
    $(".mobile").attr('class', 'icon mobile on');

}

So by default, the class is loaded with icon mobile off. Then when the span is clicked, it runs through the functions, and if it has mobile assigned to it, then it will turn the class to icon mobile on which is perfect for the first view. But then when I go to another span and it does not have a mobile, it still stays ON from before. How can I clear stuff like that on each new click?

<div class="icons">
    <div class="icon website off" data-link=""></div>
    <div class="icon mobile off" data-link=""></div>
    <div class="icon fb off" data-link=""></div>
</div>
like image 727
Drew Avatar asked Jan 30 '12 19:01

Drew


People also ask

How do I check if a variable is empty or null in jQuery?

If myvar contains any value, even null, empty string, or 0, it is not "empty". To check if a variable or property exists, eg it's been declared, though it may be not have been defined, you can use the in operator. Show activity on this post.

How do you know if you are not null?

Use the strict inequality (! ==) operator to check if a variable is not null, e.g. myVar !== null . The strict inequality operator will return true if the variable is not equal to null and false otherwise.


1 Answers

Since you are using data attributes you can use jQuery data() method to there values and optimize your code something like this.

$("#menu span").click(function() {
    var data = $(this).data();//Will get all data attribute as key/value pairs
    if (data.url) {
    }
    if (data.mobile) {
    }
    if (data.facebook) {
    }
};

If there are multiple span the you can use delegate on the #menu and look for span, this way the click handler will be attached only once on #menu element. Try this.

$("#menu").delegate('span', 'click', function() {
    var data = $(this).data();//Will get all data attribute as key/value pairs
    if (data.url) {
    }
    if (data.mobile) {
    }
    if (data.facebook) {
    }
};

Update based on OP's comment:

if (data.mobile) {
    $('.icons').attr("data-link", data.mobile)
    .removeClass('off').addClass('on');
}
else{
    $('.icons').removeAttr("data-link")
    .removeClass('on').addClass('off');
}
like image 112
ShankarSangoli Avatar answered Sep 20 '22 23:09

ShankarSangoli