Change background color if width is bigger - javascript

I want to change the background color if width is bigger than 100.
This is my code but it doesn't work.
Thanks for any help!
<html>
<head>
<style type="text/css">
div#mydiv {
width: 200px;
height: 100px;
background-color: red;
}
</style>
<script language="JavaScript">
function () {
var mydiv = document.getElementById("mydiv");
var curr_width = parseInt(mydiv.style.width);
if (curr_width > 100) {
mydiv.style.BackgroundColor = "blue";
}
}
</script>
</head>
<body>
<div id="mydiv" style=""></div>
</body>
</html>

Change
parseInt(mydiv.style.width);
mydiv.style.BackgroundColor = "blue";
To
mydiv.offsetWidth
mydiv.style.backgroundColor = "blue";

use
var curr_width = mydiv.offsetWidth;
instead
var curr_width = parseInt(mydiv.style.width);

Change:
var curr_width = parseInt(mydiv.style.width);
mydiv.style.BackgroundColor = "blue";
to:
var curr_width = mydiv.offsetWidth;
mydiv.style.backgroundColor = "blue";
I have set up a fiddle here.
Also notice I took it out of the function because it looked like it wasn't being called anywhere. You should also move the script out of the head to the bottom of the body tag or use window.onload.
UPDATE
Another fiddle with everything together

I assume this is a duplicate question.
Anyway, your intialization of curr_width need not include parseInt.
parseInt is for converting a value to integer type and here you doesnt require it.
Your code can be re-written as
<html>
<head>
<style type="text/css">
div#mydiv {
width: 200px;
height: 100px;
background-color: red;
}
</style>
<script language="JavaScript">
function () {
var mydiv = document.getElementById("mydiv");
var curr_width = mydiv.offsetWidth;
if (curr_width > 100) {
mydiv.style.BackgroundColor = "blue";
}
}
</script>
</head>
<body>
<div id="mydiv" style=""></div>
</body>
</html>

Assuming your function to be called onload. Here's the code:
<html>
<head>
<style type="text/css">
#mydiv {
width: 200px;
height: 100px;
background-color: red;
}
</style>
<script language="JavaScript">
function load(){
var mydiv = parseInt(document.getElementById("mydiv").offsetWidth);
if (mydiv > 100) {
document.getElementById("mydiv").style.backgroundColor = "blue";
}
}
</script>
</head>
<body onload="load();">
<div id="mydiv" style=""></div>
</body>
</html>
Changes:
Use offsetWidth to get the width of the div.
Use backgroundColor instead of BackgroundColor.

To get a proper computed width, you need to use the (not enough used) method getBoundingClientRect() https://developer.mozilla.org/en-US/docs/Web/API/element.getBoundingClientRect
Latest browsers have .width property, otherwise you just need to take right - left to get it.
Some comments:
- language="JavaScript" is useless. Like type="text/javascript". It's the default behavior. Seriously.
- you need to execute your code after the div has been created. So using onload or just by calling the code after in the html (like in my example)
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#mydiv {
width: 200px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div id="mydiv"></div>
<script>
/* run the code after the creation of #mydiv */
var mydiv = document.getElementById("mydiv");
var clientRect = mydiv.getBoundingClientRect()
var curr_width = clientRect.width || (clientRect.right - clientRect.left);
if (curr_width > 100) {
mydiv.style.backgroundColor = "blue";
}
</script>
</body>
</html>
Here is a working example http://jsbin.com/xapet/1/edit
Warning: to do this properly it's recommended that you execute this code each time the browser is resized.
Maybe you can take a look to the "element queries" thing, that will be a nice workaround according to media queries limitations.
https://encrypted.google.com/search?hl=en&q=element%20queries%20css

Related

Onchange color picker not changing canvas background color

I'm trying to change the background of my canvas element for a school project based on user input from the color-picker. However I can't seem to get it to work. I've checked over the code and everything seems to be properly selected. Any ideas on why?
docolor() {
var myCanvas = document.querySelector("#myCanvas");
var colorinput = document.querySelector("#clr");
var color = colorinput.value;
myCanvas.style.backgroundColor = color;
}
<head>
<title> canvas practice </title>
</head>
<body>
<canvas id = "myCanvas">
</canvas>
<br>
<input type = "color" value = "#001A57" id = "clr" onchange = "docolor()">
</body>
</html>
-
#clr {
display: inline-block;
}
#myCanvas {
height: 150px;
width: 300px;
border: 2px solid;
}
docolor isn't properly defined, so it never runs. Declare your function like this and it should work:
function docolor() {
var myCanvas = document.querySelector("#myCanvas");
var colorinput = document.querySelector("#clr");
var color = colorinput.value;
myCanvas.style.backgroundColor = color;
}
Let me know if it's still not working. Here's a link to a working codepen if you want to see everything working together. Goodluck with your project!
It looks you forgot the function declaretion.
<html>
<head>
<title> canvas practice </title>
<style>
#clr {
display: inline-block;
}
#myCanvas {
height: 150px;
width: 300px;
border: 2px solid;
}
</style>
</head>
<body>
<canvas id="myCanvas">
</canvas>
<br>
<input type="color" value="#001A57" id="clr" onchange="docolor()">
</body>
<script>
function docolor() {
var myCanvas = document.querySelector("#myCanvas");
var colorinput = document.querySelector("#clr");
var color = colorinput.value;
myCanvas.style.backgroundColor = color;
}
</script>
</html>

Working with CSS, top propery in JQuery

I want to change top position of class bbb after 100 ms, but it took out that .css(top) does not work.
Please help.
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.2.1.min.js"></script>
</head>
<body>
<div class="ddd"><div class='bbb'>Bobobo</div></div>
</body>
<script>
$(function myFunction() {
setInterval(alertFunc, 100);
});
function alertFunc() {
var b = $('.bbb').first();
b.css('top', 100 + 'px');
}
</script>
</html>
You should use setTimeout() instead of setInterval() this way alertFun() will only run once.
let alertFunc = function() {
$('.bbb').css('top', 100 + 'px');
}
setTimeout(alertFunc, 100);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ddd">
<div class='bbb'>Bobobo</div>
</div>
Just like #jhpratt mentioned. You need to add position:relative to the .bbb class.
See below.
$(function myFunction() {
setTimeout(alertFunc, 500);
});
function alertFunc() {
var b = $('.bbb').first();
b.css('top', 100 + 'px');
}
.bbb {
position: relative;
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="ddd">
<div class='bbb'>Bobobo</div>
</div>
As others have already said, if you want to use the top attribute you need to give
position:relative;
to the element. This way the element will be set relative to his position and this could be a little tricky. By the way i usually prefer to make a container box relative ad put the positionable element absolute in it, so it will be displayed relative to his container:
.container{
position:relative;
}
.element{
position:absolute;
}
This is the html:
<div class="container">
<div class="element"></div>
</div>

How to manipulate div style with javascript?

<!DOCTYPE html>
<html>
<head>
<title>
</title>
<style>
div
{
width:5em;
height:5em;
background-color:yellow;
}
</style>
</head>
<body>
<div id="div">
</div>
<script>
window.onload = function() {
var x;
x = document.getElementByID("div");
x.style.width = '9em' ;
x.style.backgroundColor = "green";
};
</script>
</body>
</html>
What im doing wrong ? I want to change div properties like width or background color with javascript but it's not working.
change getElementByID to getElementById
<!DOCTYPE html>
<html>
<head>
<title>
</title>
<style>
div
{
width:5em;
height:5em;
background-color:yellow;
}
</style>
</head>
<body>
<div id="div">
</div>
<script>
window.onload = function() {
var x;
x = document.getElementById("div");
x.style.width = '9em' ;
x.style.backgroundColor = "green";
};
</script>
</body>
</html>
You're using getElementByID. The correct method is getElementById.
Your script should look something like this:
window.onload = function() {
var x;
x = document.getElementById("div");
x.style.width = '9em';
x.style.backgroundColor = "green";
};
JavaScript Syntax Error ! Here is the update...........
<!DOCTYPE html>
<html>
<head>
<title>
</title>
<style>
/* div {
width: 5em;
height: 5em;
background-color: yellow;
} */
</style>
</head>
<body>
<div id="div">
</div>
<script>
var x = document.getElementById("div");
x.style.width = "9em";
x.style.height = "9em";
x.style.display = "block";
x.style.backgroundColor = "green";
</script>
</body>
</html>
Change getElementByID to getElementById.
window.onload = function() {
var x;
x = document.getElementById("div");
x.style.width = '9em';
x.style.backgroundColor = "green";
};
If you want to search by the tag name, you can directly use getElementsByTagName("div") instead of assigning an id of the value div. But bear in my mind that it will return all div elements; i.e. a collection of elements.

How to Change a div width when size of browser window change with javascript

How to Change a div width when size of browser window change with javascript (no jQuery)?
I want to perform this job dynamically when user resize his browser.
Please help, Immediately ...
any suggestion for this job with css?
Use percentage. for example width="50%" This will change the width when browser size change.
You can either set this with CSS or Javscript
CSS would be easily done using %'s ie
div {
width: 95%;
}
JS would be easily done using
var element = document.getElementById("x");
window.addEventListener('resize', function(event) {
element.style.width = window.style.width;
});
This code should work in all browsers. But you really should use CSS instead.
<!DOCTYPE html >
<html>
<head>
<meta charset="utf8" />
<title>untitled</title>
</head>
<body>
<div id="myDiv" style=" height: 200px; margin:10px auto; background: green;"></div>
<script type="text/javascript">
var myElement = document.getElementById('myDiv');
function detectWidth(){
var myWidth = 0;
if(typeof (window.innerWidth)== 'number'){
myWidth = window.innerWidth;
}
else {
myWidth = document.documentElement.clientWidth; //IE7
}
myElement.style.width=myWidth-300+'px';
}
window.onload=function(){
detectWidth();
};
window.onresize = function (){
detectWidth();
};
</script>
</body>
</html>

z-index property not getting retrieved while other properties are getting retrieved by the same helper function

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="20000;http://new-url/" id="meta-refresh">
<style type="text/css">
#test{
width: 100px;
height: 80px;
background-color: yellow;
opacity:0.5;
z-index:3;
}
</style>
<title>Add Properties</title>
<!--link rel="stylesheet" href="qunit-1.12.0.css"-->
</head>
<body>
<div id="test">This is some text</div>
<p>Properties</p>
<script>
function getStyle(el, cssprop){
if (el.currentStyle) //IE
return el.currentStyle[cssprop]
else if (document.defaultView && document.defaultView.getComputedStyle) //Firefox
return document.defaultView.getComputedStyle(el, "")[cssprop]
else //try and get inline style
return el.style[cssprop]
}
console.log("1"+navigator.appVersion);
console.log("2"+navigator.platform);
console.log("3"+history.length);
console.log("4"+parent.top.document.referrer);
metatags = document.getElementsByTagName("meta");
var content = metatags[0].getAttribute("content");
var mr = document.getElementById("meta-refresh");
console.log("Meta Refresh"+ content);
console.log(navigator.plugins);
console.log(navigator.plugins.length);
var mydiv = document.getElementById("test");
console.log(getStyle(mydiv,'width'));
console.log(getStyle(mydiv,'opacity'));
console.log(getStyle(mydiv,'z-index'));
var d = new Date()
var n = d.getTimezoneOffset();
console.log(n);
</script>
</body>
</html>
This is the code and all properties like width opacity show appropriate values but z-index gives out an undefined value.I tried 'z-index' as well as "zindex".Please help me with this problem.
Thanks in advance
Swaraj
I tried z-index as well as zindex
Close, but it's zIndex. Properties of CSSStyleDeclarations (such as returned by .style or getComputedStyle()) are camel-cased. You also could use .getPropertyValue("z-index").

Categories

Resources