Toggle Width of a Div Using Jquery

Zared619

I want to toggle the width of a div using a button. I'm not sure how to properly use an if-else statement to check the CSS, and I'm also not clear on the syntax.

Let's assume that I have the following:

CSS

.firstDiv {
    border:1px solid black;
    padding:10px;
    width: 80px;
    margin:5px;
    display:relative;
}

HTML

<button id="boxToggle">Change Width</button>
<div class="firstDiv">
    <p>This is a paragraph.</p>
</div>

JavaScript

$("#boxToggle").click(function () {
    $("#firstDiv").css("width", "120px");
});

Right now, the JavaScript would only change the width once and not go back, but this still isn't working and I'm assuming that my syntax is wrong.

A JSFiddle of the above can be found at the link.

https://jsfiddle.net/647ye1pk/

Any help is appreciated.

Akshay

You can use toggleClass to achieve this effect

//I need an "if-else" statement in here, but I'm not sure how to use css as a condition
$("#boxToggle").click(function () {
    $(".firstDiv").toggleClass('largeWidth');
});
.firstDiv {
    border:1px solid black;
    padding:10px;
    width: 80px;
    margin:5px;
    display:relative;
}

.largeWidth{
    width:120px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<button id="boxToggle">Change Width</button>
<div class="firstDiv">
    <p>This is a paragraph.</p>
</div>

And if you don't want to use the toggleClass method try this

//I need an "if-else" statement in here, but I'm not sure how to use css as a condition
click = 0;
$("#boxToggle").click(function () {
    if (click == 0) {
        $(".firstDiv").css("width", "120px");
        click = 1;
    } else {
        $(".firstDiv").css("width", "80px");
        click = 0;
    }
});
.firstDiv {
    border:1px solid black;
    padding:10px;
    width: 80px;
    margin:5px;
    display:relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<button id="boxToggle">Change Width</button>
<div class="firstDiv">
    <p>This is a paragraph.</p>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Toggle Width of a Div Using Jquery

From Dev

Toggle width of a container using jQuery

From Dev

Using JQuery to toggle div visibility

From Dev

Not able to toggle <div> using jQuery

From Dev

Making div invisible and visible using toggle in jQuery

From Dev

JQuery Toggle Navigation Bar Using Div

From Dev

JQuery clickable div to toggle content using Wildcard

From Dev

Animate div using jQuery and changing the Width

From Dev

centering a div with dynamic width using javascript/jquery?

From Dev

Animating the width of a div using percentages in jquery

From Dev

How to get width of a div in percentage using jQuery?

From Dev

Setting the width of a div class using jQuery not working

From Dev

Animating the width of a div using percentages in jquery

From Dev

Animating width of div and limiting to max-width using jquery

From Dev

jQuery toggle 3 Div

From Dev

jQuery Div Toggle Switcher

From Dev

jQuery toggle 3 Div

From Dev

jQuery Div Toggle Issue

From Dev

jQuery Toggle div and class

From Dev

How Toggle div in jquery?

From Dev

Animate toggle margin-left of div using jQuery?

From Dev

Jquery Toggle div on clicking Panel header using + and - symbol

From Dev

How to toggle my div content independently using a single jquery script

From Dev

Toggle div using javascript

From Dev

Problems using JQuery Toggle

From Dev

toggle checkbox using jquery

From Dev

Animate width toggle from right to left in jQuery

From Dev

how to get max-width available for a div by using jquery

From Dev

How to change <div>'s width by user input using jQuery/JavaScript?