Solved

trying to get a better way to do it jquery

Posted on 2013-12-18
2
211 Views
Last Modified: 2013-12-18
I am doing like this:

Is there a better way to this, a lot of check conditions are, like if i can use something like each and one liner could do

http://jsfiddle.net/BsfZF/
0
Comment
2 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 39726105
Hi,
a more generic way would be:
- Extend the li elements by using a custom data attribute
- Use the above attribute to set the classes

HTML:
<div>
    <ul>
        <li class="display" id="ID1" data-switch="class0,class1">Data 1</li>
        <li class="display" id="ID2"data-switch="class2">Data 2</li>
        <li class="display" id="ID3" data-switch="class3">Data 3</li>
    </ul>
</div>
<table>
<tr>
    <td class="class0">&nbsp;</td>
    <td class="class1">abc</td>
    <td class="class2">xyz</td>
    <td class="class3">xyzx</td>
</tr>
<table>

Open in new window

Script:
$(document).ready(function() {
    $('div ul li.display').click(function() {
        var li = $(this).attr('data-switch').split(",");
        $.each(li, function(key,value) {
            $("." + value).toggleClass("hidden hideBorder");
        });
    });
});

Open in new window

Online sample:
http://jsfiddle.net/EE_RainerJ/rv2ys/

HTH
Rainer
0
 
LVL 15

Author Closing Comment

by:Gurpreet Singh Randhawa
ID: 39726256
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now