I want to toggle visibility of multiple divs using knockout. Below is the rough idea of my problem –
<button>Button 1</button>
<button>Button 2</button>
<button>Button 3</button>
<div> Div 1 </div>
<div> Div 2 </div>
<div> Div 3 </div>
By default, ‘Div 1’ should be visible.
When I click individual buttons it should display only the related divs based on the buttons clicked.
I have gone through the Knockout live examples but not getting how to do this efficiently.
Please help!
The following will do a job for you. It’s not ideal, but should give you a platform to work on.
First, everything in Knockout is tied to a view model. You want to be able to control the visibility of 3 divs, so here’s a view model which might suit. Like I said, not perfect 🙂
You’ll need to change your markup to:-
Couple of things to note here. First, I’ve added the type attribute. Without it, the default behaviour of the button will be to try and submit your form.
Tying it all up:-