admin管理员组文章数量:1326447
I am trying to hide a button (not inside form tags) after it has been clicked. Once the form is shown, there is no use for the button. So i would like to hide it after clicked
Here's the existing code.
<script type="text/javascript">
$(function(){
var button = document.getElementById("info");
var myDiv = document.getElementById("myDiv");
function show() {
myDiv.style.visibility = "visible";
}
function hide() {
myDiv.style.visibility = "hidden";
}
function toggle() {
if (myDiv.style.visibility === "hidden") {
show();
} else {
hide();
}
}
hide();
button.addEventListener("click", toggle, false);
});
</script>
<input id="info" type="button" value="Имате Въпрос?" class="switchbuton">
I am trying to hide a button (not inside form tags) after it has been clicked. Once the form is shown, there is no use for the button. So i would like to hide it after clicked
Here's the existing code.
<script type="text/javascript">
$(function(){
var button = document.getElementById("info");
var myDiv = document.getElementById("myDiv");
function show() {
myDiv.style.visibility = "visible";
}
function hide() {
myDiv.style.visibility = "hidden";
}
function toggle() {
if (myDiv.style.visibility === "hidden") {
show();
} else {
hide();
}
}
hide();
button.addEventListener("click", toggle, false);
});
</script>
<input id="info" type="button" value="Имате Въпрос?" class="switchbuton">
Share
Improve this question
edited Apr 30, 2015 at 9:03
ivan.mylyanyk
2,1014 gold badges30 silver badges39 bronze badges
asked Apr 30, 2015 at 8:46
Mo-AlantehMo-Alanteh
3613 gold badges4 silver badges11 bronze badges
4
-
By the way, I suppose you are using the third-party library for switch button, so, the class attribute might be
"switchbutton"
. – ivan.mylyanyk Commented Apr 30, 2015 at 8:49 - Also, please, describe what exactly doesn't work (do you receive any error, or your block just doesn't hide) – ivan.mylyanyk Commented Apr 30, 2015 at 8:51
- Does this suits your requirement... jsfiddle/5epLf928/1 ? – Rakesh_Kumar Commented Apr 30, 2015 at 9:00
- I think the problem is in how you're wrapping the function... look at uzay's answer – maioman Commented Apr 30, 2015 at 9:15
3 Answers
Reset to default 2You can use jQuery hide
$("#myDiv").hide() // to hide the div
and show like
$("#myDiv").show() // to show the div
Or toggle to toggle the visibility of dom elements
$("#myDiv").toggle() // to toggle the visibility
You can check the result here:
http://jsfiddle/jsfiddleCem/33axo20f/2/
Code is:
<style>
.showButon{
background:url('http://spacetelescope.github.io/understanding-json-schema/_static/pass.png');
background-repeat:repeat-y;
height:30px;
text-indent:20px;
}
</style>
<div id="myDiv">
<input id="info" type="button" value="Имате Въпрос?" class="showButon" />
</div>
(function(){
var button = document.getElementById("info");
var myDiv = document.getElementById("myDiv");
function toggle() {
if (myDiv.style.visibility === "hidden") {
myDiv.style.visibility = "visible";
} else {
myDiv.style.visibility = "hidden";
}
}
button.addEventListener("click", toggle, false);
})()
Why don't you use:
<script type="text/javascript">
$(function(){
$('#info').click(function() {
$(this).hide();
});
});
</script>
<input id="info" type="button" value="Имате Въпрос?" class="switchbuton">
本文标签: javascriptHide button after clickedStack Overflow
版权声明:本文标题:javascript - Hide button after clicked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742213736a2434184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论