button needs to be clicked twice to make trigger the script
I have the following problem: I have to click on the button twice to make the script work, and I don't know why. I've tried to read some posts with the search but none of them have resolved my question.
Thanks for the help.
#div1{width: 400px; background-color: beige; transition: 1s}
.div2{background-color: aquamarine}
.div2 button{float: right; width: 50px;cursor: pointer; outline: none}
.div2 p{height: 0px;overflow: hidden;transition: 1s}
.div3 img{padding: 5%}
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
javascript html
add a comment |
I have the following problem: I have to click on the button twice to make the script work, and I don't know why. I've tried to read some posts with the search but none of them have resolved my question.
Thanks for the help.
#div1{width: 400px; background-color: beige; transition: 1s}
.div2{background-color: aquamarine}
.div2 button{float: right; width: 50px;cursor: pointer; outline: none}
.div2 p{height: 0px;overflow: hidden;transition: 1s}
.div3 img{padding: 5%}
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
javascript html
add a comment |
I have the following problem: I have to click on the button twice to make the script work, and I don't know why. I've tried to read some posts with the search but none of them have resolved my question.
Thanks for the help.
#div1{width: 400px; background-color: beige; transition: 1s}
.div2{background-color: aquamarine}
.div2 button{float: right; width: 50px;cursor: pointer; outline: none}
.div2 p{height: 0px;overflow: hidden;transition: 1s}
.div3 img{padding: 5%}
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
javascript html
I have the following problem: I have to click on the button twice to make the script work, and I don't know why. I've tried to read some posts with the search but none of them have resolved my question.
Thanks for the help.
#div1{width: 400px; background-color: beige; transition: 1s}
.div2{background-color: aquamarine}
.div2 button{float: right; width: 50px;cursor: pointer; outline: none}
.div2 p{height: 0px;overflow: hidden;transition: 1s}
.div3 img{padding: 5%}
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
#div1{width: 400px; background-color: beige; transition: 1s}
.div2{background-color: aquamarine}
.div2 button{float: right; width: 50px;cursor: pointer; outline: none}
.div2 p{height: 0px;overflow: hidden;transition: 1s}
.div3 img{padding: 5%}
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
#div1{width: 400px; background-color: beige; transition: 1s}
.div2{background-color: aquamarine}
.div2 button{float: right; width: 50px;cursor: pointer; outline: none}
.div2 p{height: 0px;overflow: hidden;transition: 1s}
.div3 img{padding: 5%}
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
javascript html
javascript html
edited Nov 23 '18 at 3:06
Juan
65112
65112
asked Nov 22 '18 at 21:25
MattiaMattia
43
43
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
On first time it goes inside the else block of your function.I solved adding style="height:0" on the
tag.
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext" style="height:0">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum,
lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
add a comment |
Your code checks the style property for the ptext element which refers only to inline styling. Since the height is applied by a CSS class and not as inline styling.
As suggested by mattia you can add the height as inline styling from the get-go, or if you prefer to use CSS as you currently are, a possible solution is posed in this question.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438135%2fbutton-needs-to-be-clicked-twice-to-make-trigger-the-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
On first time it goes inside the else block of your function.I solved adding style="height:0" on the
tag.
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext" style="height:0">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum,
lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
add a comment |
On first time it goes inside the else block of your function.I solved adding style="height:0" on the
tag.
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext" style="height:0">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum,
lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
add a comment |
On first time it goes inside the else block of your function.I solved adding style="height:0" on the
tag.
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext" style="height:0">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum,
lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
On first time it goes inside the else block of your function.I solved adding style="height:0" on the
tag.
<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext" style="height:0">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum,
lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>
edited Nov 22 '18 at 22:16
answered Nov 22 '18 at 21:34
KurohigeKurohige
5901520
5901520
add a comment |
add a comment |
Your code checks the style property for the ptext element which refers only to inline styling. Since the height is applied by a CSS class and not as inline styling.
As suggested by mattia you can add the height as inline styling from the get-go, or if you prefer to use CSS as you currently are, a possible solution is posed in this question.
add a comment |
Your code checks the style property for the ptext element which refers only to inline styling. Since the height is applied by a CSS class and not as inline styling.
As suggested by mattia you can add the height as inline styling from the get-go, or if you prefer to use CSS as you currently are, a possible solution is posed in this question.
add a comment |
Your code checks the style property for the ptext element which refers only to inline styling. Since the height is applied by a CSS class and not as inline styling.
As suggested by mattia you can add the height as inline styling from the get-go, or if you prefer to use CSS as you currently are, a possible solution is posed in this question.
Your code checks the style property for the ptext element which refers only to inline styling. Since the height is applied by a CSS class and not as inline styling.
As suggested by mattia you can add the height as inline styling from the get-go, or if you prefer to use CSS as you currently are, a possible solution is posed in this question.
answered Nov 23 '18 at 0:19
JuanJuan
65112
65112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438135%2fbutton-needs-to-be-clicked-twice-to-make-trigger-the-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown