How to Hide a HTML Button - javascript

I have a button that calls a function when it is clicked. I want it to hide when some one clicks on it.
HTML:
<DOCTYPE html>
<html>
<head>
<title>Example</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<p id="Displayer"></p>
<button onclick="Set()">Ready?</button>
<script type="text/javascript" src="javascript.js">
</script>
</body>
</html>

Pass in your element and then set the display style to none:
HTML:
<button onclick="set(this)">Ready?</button>
Javascript:
function set(elem) {
elem.style.display = "none";
}

Related

Button text change before appearing in the new html page

I have a problem: I am changing the text of a button depending on a click from the previous page, so I tried using JavaScript to update the innerHTML of the button in the new page using the onload() event for the body of the new page. But, it appears for the user when it changes its text. I just want it to appear directly without the change being noticeable to the user. Has anyone got the answer for this?
function myFunction(){
var x=document.getElementById("GymName");
x.innerHTML="hesham";
}
<!DOCTYPE html>
<html>
<head>
<link rel= 'stylesheet' href='css/bootstrap.css' />
<link rel= 'stylesheet' href='css/font-awesome.min.css' />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
</head>
<body onload="myFunction()" >
<button class="btn btn-danger" id="GymName">Omar</button>
<script src="js/jquery-2.1.4.min.js"> </script>
<script src="js/jquery.mobile-1.4.5.min.js"> </script>
<script src="js/bootstrap.min.js"> </script>
<script src="js/login.js"></script>
</body>
</html>
Start the button hidden with display: none, and only show it after the value is changed. Or, if you're looking for a smoother transition you could fade it in with jQuery.
function myFunction(){
var x = document.getElementById("GymName");
x.innerHTML = "hesham";
x.style.display = "inline";
}
#GymName {
display: none;
}
<body onload="myFunction()" >
<button class="btn btn-danger" id="GymName">Omar</button>
</body>
Make the function invoke immediately as it's defined:
(function () {
document.getElementById("GymName").innerHTML = 'hesham';
}());
As in:
<!DOCTYPE html>
<html>
<head>
<link rel= 'stylesheet' href='css/bootstrap.css' />
<link rel= 'stylesheet' href='css/font-awesome.min.css' />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
</head>
<body>
<button class="btn btn-danger" id="GymName">Omar</button>
<script>
(function () { document.getElementById("GymName").innerHTML = 'hesham'; }());
</script>
<script src="js/jquery-2.1.4.min.js"> </script>
<script src="js/jquery.mobile-1.4.5.min.js"> </script>
<script src="js/bootstrap.min.js"> </script>
<script src="js/login.js"></script>
</body>
</html>

Unable To Change CSS JavaScript

I just want to set display property of one of my div to block based on event, by default its 'none'
Its not happening with error: Cannot set property 'display' of undefined
Below is my code. Please suggest.
HTML
<!DOCTYPE html>
<html >
<head>
<meta charset="ISO-8859-1">
<title>Sample Mod Pop UP</title>
<link rel="stylesheet" href="css/main.css" />
</head>
<body class="body">
<div class="popUp" id="id1">
<p>Are You Sure You Want to Quit?</p>
<span>&#10006</span>
</div>
<button onClick=closeWindown() class="btn">Close</button>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>
JavaScript
function closeWindown(){
document.getElementsByClassName("popUp").style.display="block";
}
getElementsByClassName gets a nodeList, so you have to iterate
function closeWindown(){
var elements = document.getElementsByClassName("popUp");
for (var i=elements.length; i--;) {
elements[i].style.display = "block";
}
}
As a sidenote, querySelectorAll('.popup') has better support, but is non-live.

Why my Jquery popup plugin isn't working?

I'm using this magnific pop up plug in (http://dimsemenov.com/plugins/magnific-popup/documentation.html)
This is my very basic html, I simply want the button to open a pop up window in that page.
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</script>-->
<script src="./js/jquery-1.11.1.min.js"></script>
<script src="./js/jquery.magnific-popup.js"></script>
<title>Burn That Burger</title>
<script type="text/javascript" src="js/javascript.js"></script>
<link rel="stylesheet" type="text/css" href="normalize.css" media="all">
<link rel="stylesheet" href="style/unsemantic-grid-responsive.css" />
<link rel="stylesheet" type="text/css" href="style/style.css" media="all">
</head>
<body>
<div id="popup">
test pop up goes here
</div>
<button>create and show popup</button>
</body>
</html>
Javascript:
$(document).ready(function () {
$('button').magnificPopup({
items: {
src: '#popup',
type: 'inline'
}
});
});
JSFiddle: http://jsfiddle.net/TrueBlueAussie/vGAsL/19/
you have applied the popup id to your body, instead of a separate div.
<body>
<div id="popup">
test pop up goes here
</div>
<button> create and show popup </button>
I think you have an issue in your body tag, it should be:
<body>
<div id="popup">
test pop up goes here
</div>

Button Failing to Perform Function jQuery

I'm trying to make a button, so that when pressed it slides one div away and then another slides back in, however I'm trying to prevent it from refreshing the page but the
return false;
line seems to break the script. Any ideas?
HTML:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript" src="slide.js"></script>
</head>
<body>
<button id="button1" onclick="nextSlide('#1', '#2')">Hide it</button>
<p id="1">Hide this text</p>
<p id="2">Show this text</p>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
</body>
</html>
JS:
window.onload=function(){
$('#2').fadeOut(1);
};
function nextSlide(hideDiv, showDiv) {
$(hideDiv).hide("slide", 2000, function(){
$(showDiv).show("slide", 2000);
});
return false;
};
Why do you have to return anything? Is there another method dependent on getting "false" back?

use external .js to call .bind()

im trying to call
.bind("click")
from an external .js file (js1.js).
here is the code:
js1.js:
$(function() {
$("p").bind("click",function(){
alert("The paragraph was clicked.");
});
});
page.html:
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="./css/mystyle.css" />
<link rel="stylesheet" href="./css/global.css">
<script type="text/javascript" src="./js/js1.js"></script>
<title>New WebPage</title>
</head>
<body>
<p>Click me!</p>
</body>
this code does not work in my files.
i found it from w3scools and it is working in their site:
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").bind("click",function(){
alert("The paragraph was clicked.");
});
});
</script>
</head>
<body>
<p>Click me!</p>
</body>
</html>
how can i make it running from my external .js? any ideas?
thanks.
By the looks of it, you haven't added jQuery on your page yet. Add jQuery prior to your script.
<link rel="stylesheet" type="text/css" media="screen" href="./css/mystyle.css" />
<link rel="stylesheet" href="./css/global.css">
<script type="text/javascript" src="path/to/jQuery.js"></script>
<script type="text/javascript" src="./js/js1.js"></script>
<title>New WebPage</title>
If you forget to add this line
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
so add it in your <head> </head> section first. Otherwise your script could not work.
Bellow the complete code which working for me..
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#p').bind('click', function(){
alert('The paragraph was clicked.');
});
});
</script>
<style>
#p{cursor:pointer;}
</style>
</head>
<body>
<p id="p">Click me!</p>
</body>
</html>

Categories