jquery mvc5 button question

Fairly new to jQuery

I have  a series of buttons that I want to use to display some other html and hide it when done.  I have this

------------------------
<div>
      <input id="DisplayData" type="button" class="gbtn" value="Display" />
</div>

<br />

<div id="data" style="display:none">
    Now is the time xxxxxxxxxxxxxxxxxxxxxxxxxx<br />
    <input id="HideData" type="button" class="gbtn" value="Hide" />
</div>

<div id="data2" style="display:none">   <------------------------------------- Second Div that I want to toggle display on and off
    Now is the time xxxxxxxxxxxxxxxxxxxxxxxxxx<br />
    <input id="HideData2" type="button" class="gbtn" value="Hide" />
</div>

<script type="text/javascript">

    $("#DisplayData").button().click(function () {
        $('#data').show("fast");
    });

    $("#HideData").button().click(function () {
        $('#data').hide("fast");
    });

</script>

----------------------------------------

My question is I have have 10 of these <div id="data">  do I need to create a separate  jQuery function for each? or can I get one function to handle all the  ID's
Charles BaldoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
All 10 can be managed by two buttons (if that is what you asked for)

 $("#DisplayData").button().click(function () {
        $('#data').show("fast");
        $('#data1').show("fast");
        // so on
    });

Open in new window

Julian HansenCommented:
You have two options

First prize - give each of the <div>'s a class and target the class
<div class="toggle-me" id="data1">...</div>
<div class="toggle-me" id="data2">...</div>
...
<div class="toggle-me" id="data10">...</div>

Open in new window


In the event handler
$('.gbtn').click(function(e) {
  e.preventDefault();
  $('.toggle-me').toggle();
});

Open in new window


Notice we are toggling based on click on a .gbtn class - if you want to hide your DisplayData button on toggle you can give it a toggle me class as well - that way it will hide when clicked and the HideData will show and vice versa.

What is not clear from your post is whether you want the HideData to hide ALL div's or only its parent. Post back if it is not all.

Another option (not using classes) is to use the wildcard selector on the data prefix of the id like this
$('[id^=data]').toggle();

Open in new window

Not my preference - I would go with the class - but if you are confined to using only id's then the above will work.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Charles BaldoSoftware DeveloperAuthor Commented:
Thank You
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.