2

I have this code :

$(document).ready(function(){
       $('#ShowUser').click(function(){
           $('.DivUser').toggle("fast");
        });
 });

 $(document).ready(function(){
       $('#ShowImage').click(function(){
           $('.DivImage').toggle("fast");
        });
 });

$(document).ready(function(){
       $('#ShowProfile').click(function(){
           $('.DivProfile').toggle("fast");
        });
 });


<label id="ShowUser">
<div class="ShowUser">

<label id="ShowImage">
<div class="ShowImage">

<label id="ShowProfile">
<div class="ShowProfile">

I want to put only in one function ,but javascript is not my strong side :) can someone help :)

2
  • <label id="Show1"/> <div class="Div1" style="display:block"> Commented Apr 23, 2014 at 6:28
  • Can you show us your HTML? There's might be a more efficient way to do that. Commented Apr 23, 2014 at 6:31

1 Answer 1

4

You can use:

$(document).ready(function () {
    $('[id^="Show"]').click(function () {
        var id = this.id.match(/\d+$/);
        $('.Div' + id).toggle("fast");
    });
});

Fiddle Demo

Sign up to request clarification or add additional context in comments.

2 Comments

it work pretty good ,but if i have something like this : <div id="ShowUser">Show1</div> <div id="ShowImage">Show2</div> <div class="DivUser">Show1</div> <div class="DivImage">Show2</div>
Felix you are genius :)

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.