javascript - Swap div content with one button -
i have 2 types content in each div. 1 table , form. want swap pressing button.
html
<a id="btn" class="btn btn-primary pull-right"><i class="fa fa-envelope"> contact us</i></a> <br style="clear:both" /> <div class="well well-sm"> <div id="table"> <table class="table table-bordered"> <tr> <th>id</th> <th>title</th> <th>price</th> </tr> <tr> <td>001</td> <td>dl650xt</td> <td>369000</td> </tr> </table> </div> <div id="formdiv"> <form class="form" method="post" action="" name="contact" id="contact"> <fieldset> <legend> contact form </legend> <div class="form-group has-feedback"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span> <input type="text" class="form-control" id="name" name="name" placeholder="name *" /> <span class="form-control-feedback glyphicon"></span> </div> </div> <div class="form-group has-feedback"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span> <input type="text" class="form-control" id="email" name="email" placeholder="e-mail *" /> <span class="form-control-feedback glyphicon"></span> </div> </div> <div class="form-group has-feedback"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span> <input type="text" class="form-control" id="telefon" name="telefon" placeholder="telefon" /> <span class="form-control-feedback glyphicon"></span> </div> </div> <div class="form-group has-feedback"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span> <textarea type="text" id="message" class="form-control" name="message" rows="10" cols="40" placeholder="nachricht*"></textarea> <span class="form-control-feedback glyphicon"></span> </div> </div> <div class="form-group"> <button type="submit" id="submit" class="btn btn-primary btn-lg">submit</button> <button type="reset" id="cancel" class="btn btn-danger btn-lg">reset 1</button> </div> </fieldset> </form> </div> </div> javascript
$(document).ready(function() { $('#formdiv').css('border', 'red !important'); //swap content $('#btn').on('click',function() { $('#table').hide(); $('#formdiv').prop('display', 'show'); }) }); it's working once. want content swap every click.
here's fiddle : https://jsfiddle.net/krm85qq3/5/
you can use toggle function in order display or hide matched elements.
$('#btn').on('click',function() { $('#table').toggle(); $('#formdiv').prop('display', 'show'); }) $(document).ready(function() { $('#btn').on('click', function() { $('#table').toggle(); $('#formdiv').toggle(); }); }); #formdiv { display: none; border: red; } <link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a id="btn" class="btn btn-primary pull-right"><i class="fa fa-envelope"> contact us</i></a> <br style="clear:both" /> <div class="well well-sm"> <div id="table"> <table class="table table-bordered"> <tr> <th>id</th> <th>title</th> <th>price</th> </tr> <tr> <td>001</td> <td>dl650xt</td> <td>369000</td> </tr> </table> </div> <div id="formdiv"> <form class="form" method="post" action="" name="contact" id="contact"> <fieldset> <legend> contact form </legend> <div class="form-group has-feedback"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span> <input type="text" class="form-control" id="name" name="name" placeholder="name *" /> <span class="form-control-feedback glyphicon"></span> </div> </div> <div class="form-group has-feedback"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span> <input type="text" class="form-control" id="email" name="email" placeholder="e-mail *" /> <span class="form-control-feedback glyphicon"></span> </div> </div> <div class="form-group has-feedback"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span> <input type="text" class="form-control" id="telefon" name="telefon" placeholder="telefon" /> <span class="form-control-feedback glyphicon"></span> </div> </div> <div class="form-group has-feedback"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span> <textarea type="text" id="message" class="form-control" name="message" rows="10" cols="40" placeholder="nachricht*"></textarea> <span class="form-control-feedback glyphicon"></span> </div> </div> <div class="form-group"> <button type="submit" id="submit" class="btn btn-primary btn-lg">submit</button> <button type="reset" id="cancel" class="btn btn-danger btn-lg">reset 1</button> </div> </fieldset> </form> </div> </div>
Comments
Post a Comment