0
$('li.' + $(this).text()).toggle(true);

This code is used to take from a subnav below.

<div class="subNav">
        <ul>
     <li class="button">sun protective clothing</li>
        </ul>
 </div>

These are products that will be visible.

<div class="sun-protective-clothing"></div>
3
  • you're want to pick all the .button li's and change their class? please specify what you're trying to accomplish Commented Jul 6, 2011 at 17:24
  • 1
    @Buildingbrick:Although there is merit that it's "easy to do" or a quick hack, but are you sure you want to go with this approach? A change in your content could render the page useless, or if someone edits the page without realizing that it's tied up to a CSS class by the same name. I suggest storing the class name as a data-* attribute: <li class="button" data-className="sun-protective-clothing">Sun Protective Clothing</li> and you could just say $('li.'+$(this).data('className')).toggle(true); You'll free up the coupling between the actual content and the class name Commented Jul 6, 2011 at 17:28
  • @Nupul: I absolutely agree with the data- attribute approach. Commented Jul 6, 2011 at 17:47

3 Answers 3

2

You must use JavaScript's replace() function to replace all spaces with dashes. Note using replace(" ", "-") like this will only replace the first instance of a space in the string. you must use a RegEx with the global search to replace all instances; such as / /g

$('li.' + $(this).text().replace(/ /g, "-")).toggle(true);
Sign up to request clarification or add additional context in comments.

Comments

0

you need to replace the spaces with dashes. Try

$('li.' + $(this).text().replace(/ /g, "-")).toggle(true);

1 Comment

This will only replace the first space. You must use a RegEx (Regular Expression).
0

I would recommend no using the text inside the li and do something like this:

<div class="subNav">
    <ul>
 <li class="button" target="sun-protective-clothing">sun protective clothing</li>
    </ul>
</div>

and:

$('.' + $(this).attr("target")).toggle(true);

This way you can put whatever you want inside the li, and not have it affect your logic.

1 Comment

or maybe rel instead of target? <li class="button" rel="sun-protective-clothing">sun protective clothing</li>, $('.' + $(this).attr("rel")).toggle(true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.