Solved

trying to get a better way to do it jquery

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

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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…

803 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