jQuery('.mybuttonclass').on('click', function(){
// Get the number from button ID.
var id = jQuery(this).attr('id').replace('button-','');
// Toggle the visibility.
jQuery("#intro-" + id).css({"display" : "none"});
jQuery("#content-" + id).css({"display" : "inline-block"});
});
IF you need the id for something else fine but for this use classes. Just use a class intro-group for a group and then hidden on elements to hide initially and toggle it, even easier, add a class toggled and use that to target the elements to toggle the class hidden on.
Now, I need to create a very big number of other div with id=”cont-x” … is it possible create a single function that you can select the right div to show/hide by clicking on a button by class?
Untill now I create a function for every button, and I retrive the dive to show/hide by ID. I would like to use a single function for all the div and select the “this” button by classname. Is it possible?
Comments
Comment posted by Mark Schultheiss
Please add comments/more information if none of the current posts answer your question.
Comment posted by Wimanicesir
And jQuery.. :p
Comment posted by wrexo
Ok, thank you for the code.. but I need to show/hide by click. For example I click the button-1 and jQuery hides “Some text…” and shows “More explicative text”. If I click again on the same button, jQuery shows “Some text…” and hides “More explicative text” and so on.. and there are a lot of buttons with theirs div to show/hide. See other users snippet, they solved my problem
Comment posted by Sfili_81
Sorry, i misunderstood your question 🙁
Comment posted by wrexo
Thanks for the code… Interesting Javascript example
Comment posted by connexo
See how much cleaner this approach is?
Comment posted by wrexo
Thank you a lot! It is what I was searching for.
Comment posted by Mark Schultheiss
@wrexo I appreciate the note however you can easily add appreciation by clicking the up-vote button on the left of any questions or answer you find useful or clicking the accept check mark for the one that answers your question.
Comment posted by stackoverflow.com/help/accepted-answer