1

when page loading i want to show 2 col-md-6 div and one button saying "click to show third div". if someone click the button, i want to toggle third div col-md-4 and change previous 2 col-md-6 to col-md-4. Change button to "click to hide 3 rd div"

HTML

<div class="container">
<h1>Hello World!</h1>
<div class="row pull-right" style="margin-bottom:10px;"> <button class="btn btn-primary" id="btncontrol">Click to Add </button> </div>
<div style="clear:both">
    <div class="row">
        <div class="col-sm-6" id="col_fld_6" style="background-color:lavender;">.col-sm-4</div>
        <div class="col-sm-6" id="col_fld_62" style="background-color:lavenderblush;">.col-sm-4</div>
        <div class="col-sm-4" style="background-color:#E6B2A4;display:none" id="last_div">.col-sm-4</div>
    </div>
</div>
3
  • Can you please share the HTML? Commented Mar 28, 2016 at 5:59
  • <div class="container"> <h1>Hello World!</h1> <div class="row pull-right" style="margin-bottom:10px;"> <button class="btn btn-primary" id="btncontrol">Click to Add </button> </div> <div style="clear:both"> <div class="row"> <div class="col-sm-6" id="col_fld_6" style="background-color:lavender;">.col-sm-4</div> <div class="col-sm-6" id="col_fld_62" style="background-color:lavenderblush;">.col-sm-4</div> <div class="col-sm-4" style="background-color:#E6B2A4;display:none" id="last_div">.col-sm-4</div> </div> </div> Commented Mar 28, 2016 at 6:02
  • i ma confused how to write jquery Commented Mar 28, 2016 at 6:03

2 Answers 2

4

Use add and removeClass

$('#btncontrol').click(function(){
  $('div[id^="col_fld"]').removeClass('col-sm-6').addClass('col-sm-4');
  $('#last_div').show();
});

https://jsfiddle.net/1boyw5oe/

or :

$('#btncontrol').click(function(){
  $('div[id^="col_fld"]').toggleClass('col-sm-6').toggleClass('col-sm-4');
  $('#last_div').toggle();
});

https://jsfiddle.net/1boyw5oe/1/

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

Comments

0

Well following is the HTML and JS you need.

FIDDLE

HTML

<div class="row all-three-div">
    <div class="col-md-6">Div 1</div>
    <div class="col-md-6">Div 2</div>
    <div class="col-md-4" style="display:none;">Div 3</div>
</div>

<button id="showThirdDiv">Click to show third Div</button>

jQuery

jQuery(document).ready(function(){
    jQuery('#showThirdDiv').click(function(){
        jQuery('.all-three-div div').removeClass('col-md-6').addClass('col-md-4').show();
    });
});

You need to add a class all-three-div in three divs' parent div.

Comments

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.