JavaScript get background color in variable [duplicate] - javascript

This question already has answers here:
why javascript this.style[property] return an empty string? [duplicate]
(2 answers)
Closed 6 years ago.
I've been trying to get a fade in and out for a background image for a site, and I have been trying to get the background color of a div into a variable, this is what I've tried:
elem = document.getElementById('nav');
bgColor = elem.style.backgroundColor;
But once I try to alert the variable bgColor like: alert(bgColor) all I get alerted is empty in the text box. I've looked around on some Stack questions and I've tried everything that is told there and it doesn't seem to put the physical color into a variable.

window.getComputedStyle is the solution. Otherwise you'll get the "direct" styles that are empty.

Related

How to Run two JS functions on one Click? [duplicate]

This question already has answers here:
How to call multiple JavaScript functions in onclick event?
(14 answers)
Closed 7 years ago.
I was trying to run two JS functions in one click using this. Can anybody show me how to do it correctly?
<button onclick="getlocation" onclick="showDiv()">Try It</button>
I tried adding a ; between the two functions but it didnt work too.
"Adding ";" semicolon didn't work, if you could help.."
It works for sure, you have to do it like that: onclick="getLocation();showDiv()"
Was not relevant in this case:
The problem could be, that both functions are setting the content of your div, so the second function will overwrite the content!
Use .innerHTML += "..."; on you second function, this will append the content to the exisitng content.

How do i add content in an div with javascript? [duplicate]

This question already has answers here:
How can I change div content with JavaScript?
(6 answers)
Closed 7 years ago.
How do i change the content of an div on an website that has an id with javascript?
<div class="chat-area" id="chat-area">[Changeable]</div>
I want to know how i add content where it says "[Changeable]" with javascript so i can run the command through the console.
I'd like if you keep your explainage simple, because im still very new to html/css/javascript ;)
In very simple JavaScript terms, you can use:
document.getElementById("chat-area").innerHTML = 'New Content';
And yes, this would work in GreaseMonkey / UserScripts too!
You can use the innerHTML property to achieve your goal like so:
document.getElementById("chat-area").innerHTML = "Your new content here";

I want to get the text of an html element and put it in a JavaScript variable [duplicate]

This question already has answers here:
Assign div text to variable then show it
(3 answers)
Closed 8 years ago.
I have the following div:
<div id="mydiv">something</div>
I need to get the content of the div (mydiv) and put it into a JavaScript variable. How can i do it?
var content = document.getElementById("mydiv").innerHTML;

Changing images in Javascript [duplicate]

This question already has answers here:
How to fade loop background images?
(3 answers)
Closed 8 years ago.
I have this script that changes images fine, but not smoothly. How can I change this so they change smoothly?
//--------AutoPlay select--------------------------
function autoPlaySlider(id){
var recNumber = parseInt(id)+1;
if(jQuery("#simg_"+recNumber).length){
jQuery("#rt-header-surround")
.attr("style",'background-image:url('+jQuery("#simg_"+recNumber).attr("imgpath")+')');
}else{
jQuery("#rt-header-surround")
.fadeOut()
.attr("style",'background-image:url('+jQuery("#simg_"+lastone).attr("imgpath")+')');
}
}
$().fadeOut(); applies to the element, not it's background.
If you want to animate CSS properties your best bet is to use jQuery.animate();

Element destination [duplicate]

This question already has answers here:
Retrieve the position (X,Y) of an HTML element
(30 answers)
Closed 9 years ago.
I created a html page and inserted a textbox. I would like to know a textbox destination in the page from javascript function? Example textbox's top is 100 px like that. Thank you.
Try this:
window.onload = function() {
console.log(document.getElementById("id1").offsetTop);
};
where id1 is the id of the textbox you need to triangulate.

Categories

Resources