Solved

trying to get a better way to do it jquery

Posted on 2013-12-18
2
216 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 16

Author Closing Comment

by:Gurpreet Singh Randhawa
ID: 39726256
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

685 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