简体   繁体   中英

Changing an element's class at the click of a button and renaming the button name and vice-versa

I need your help,

How can I re-minimize (set the textarea's class back to normal) when I reclick on the button "expand"

I can get it to work, but just not the other way around.

Here is my HTML + Javascript

<!DOCTYPE html>

<html>

<head>

<style type="text/css">
.normal {
    width: 400px;
    height: 25px;

}
.expand {
    height: 400px;
    width: 400px;
}
</style>

<script type="text/javascript">
function expand() {

document.getElementById('subject_area').className = "expand"

document.getElementById('expand').value = "minimize"


}
</script>

</head>

<body>
<input class="normal" id="subject_area" type="textarea" >
<input id="expand" type="button" value="expand" onclick="expand()">
</body>

</html>

You can do:

var subject = document.getElementById('subject_area');
var btnExpand = document.getElementById('expand');
subject.className == "expand" ? subject.className = "normal" : subject.className = "expand";
btnExpand.value == "minimize" ? btnExpand.value = "expand" : btnExpand.value = "minimize";
var subject = document.getElementById('subject_area');
var button = document.getElementById('expand');

function expand() {
    if (subject.className === 'normal') {
        subject.className = 'expand';
        button.value = 'minimize';
    } else {
        subject.className = 'normal';
        button.value = 'expand';
    }
}

Fiddle here: http://jsfiddle.net/ToddT/aPqgh/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM