Change H1 background colour [duplicate]
This question already has an answer here:
Dynamically access object property using variable
11 answers
I want to change the background colour of h1
tag . But I don't know why this code is not working. Please someone help me.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1 id = "h1">This is h1 tag</h1>
<script>
var h1 = document.getElementById('h1');
var x = 'background';
h1.style.x = "red";
</script>
</body>
</html>
javascript
marked as duplicate by Mike Cluck
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 21:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Dynamically access object property using variable
11 answers
I want to change the background colour of h1
tag . But I don't know why this code is not working. Please someone help me.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1 id = "h1">This is h1 tag</h1>
<script>
var h1 = document.getElementById('h1');
var x = 'background';
h1.style.x = "red";
</script>
</body>
</html>
javascript
marked as duplicate by Mike Cluck
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 21:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
probably because it should be like thisstyle[x]
– Temani Afif
Nov 14 '18 at 21:49
add a comment |
This question already has an answer here:
Dynamically access object property using variable
11 answers
I want to change the background colour of h1
tag . But I don't know why this code is not working. Please someone help me.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1 id = "h1">This is h1 tag</h1>
<script>
var h1 = document.getElementById('h1');
var x = 'background';
h1.style.x = "red";
</script>
</body>
</html>
javascript
This question already has an answer here:
Dynamically access object property using variable
11 answers
I want to change the background colour of h1
tag . But I don't know why this code is not working. Please someone help me.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1 id = "h1">This is h1 tag</h1>
<script>
var h1 = document.getElementById('h1');
var x = 'background';
h1.style.x = "red";
</script>
</body>
</html>
This question already has an answer here:
Dynamically access object property using variable
11 answers
javascript
javascript
edited Nov 14 '18 at 23:36
freedomn-m
12.7k31944
12.7k31944
asked Nov 14 '18 at 21:47
Nasir KhanNasir Khan
61
61
marked as duplicate by Mike Cluck
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 21:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Mike Cluck
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 14 '18 at 21:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
probably because it should be like thisstyle[x]
– Temani Afif
Nov 14 '18 at 21:49
add a comment |
2
probably because it should be like thisstyle[x]
– Temani Afif
Nov 14 '18 at 21:49
2
2
probably because it should be like this
style[x]
– Temani Afif
Nov 14 '18 at 21:49
probably because it should be like this
style[x]
– Temani Afif
Nov 14 '18 at 21:49
add a comment |
1 Answer
1
active
oldest
votes
In vanilla JavaScript, you do styles
is an object, so in order to access the different keys you will need to do so with array notation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1 id = "h1">This is h1 tag</h1>
<script>
var h1 = document.getElementById('h1');
var x = 'background';
h1.style[x] = "red";
</script>
</body>
</html>
Changing your call to h1.style[x]
sets the property the way you're looking for.
So wouldh1.style['background'] = "red";
also be valid? (I assume so)
– GrumpyCrouton
Nov 14 '18 at 21:53
It absolutely would!
– Ryan
Nov 14 '18 at 21:53
That's neat. Though I don't understand whyh1.style.background = "red"
would be a more popular choice given that - I guess maybe the fact it uses 3 symbols less. The other notation just seems more verbose to me
– GrumpyCrouton
Nov 14 '18 at 21:54
@GrumpyCrouton Object Oriented (known notation in most langages)
– Temani Afif
Nov 14 '18 at 21:55
1
@Tarabass I've updated my answer to reflect that - thanks for the suggestion!
– Ryan
Nov 14 '18 at 22:12
|
show 16 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In vanilla JavaScript, you do styles
is an object, so in order to access the different keys you will need to do so with array notation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1 id = "h1">This is h1 tag</h1>
<script>
var h1 = document.getElementById('h1');
var x = 'background';
h1.style[x] = "red";
</script>
</body>
</html>
Changing your call to h1.style[x]
sets the property the way you're looking for.
So wouldh1.style['background'] = "red";
also be valid? (I assume so)
– GrumpyCrouton
Nov 14 '18 at 21:53
It absolutely would!
– Ryan
Nov 14 '18 at 21:53
That's neat. Though I don't understand whyh1.style.background = "red"
would be a more popular choice given that - I guess maybe the fact it uses 3 symbols less. The other notation just seems more verbose to me
– GrumpyCrouton
Nov 14 '18 at 21:54
@GrumpyCrouton Object Oriented (known notation in most langages)
– Temani Afif
Nov 14 '18 at 21:55
1
@Tarabass I've updated my answer to reflect that - thanks for the suggestion!
– Ryan
Nov 14 '18 at 22:12
|
show 16 more comments
In vanilla JavaScript, you do styles
is an object, so in order to access the different keys you will need to do so with array notation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1 id = "h1">This is h1 tag</h1>
<script>
var h1 = document.getElementById('h1');
var x = 'background';
h1.style[x] = "red";
</script>
</body>
</html>
Changing your call to h1.style[x]
sets the property the way you're looking for.
So wouldh1.style['background'] = "red";
also be valid? (I assume so)
– GrumpyCrouton
Nov 14 '18 at 21:53
It absolutely would!
– Ryan
Nov 14 '18 at 21:53
That's neat. Though I don't understand whyh1.style.background = "red"
would be a more popular choice given that - I guess maybe the fact it uses 3 symbols less. The other notation just seems more verbose to me
– GrumpyCrouton
Nov 14 '18 at 21:54
@GrumpyCrouton Object Oriented (known notation in most langages)
– Temani Afif
Nov 14 '18 at 21:55
1
@Tarabass I've updated my answer to reflect that - thanks for the suggestion!
– Ryan
Nov 14 '18 at 22:12
|
show 16 more comments
In vanilla JavaScript, you do styles
is an object, so in order to access the different keys you will need to do so with array notation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1 id = "h1">This is h1 tag</h1>
<script>
var h1 = document.getElementById('h1');
var x = 'background';
h1.style[x] = "red";
</script>
</body>
</html>
Changing your call to h1.style[x]
sets the property the way you're looking for.
In vanilla JavaScript, you do styles
is an object, so in order to access the different keys you will need to do so with array notation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1 id = "h1">This is h1 tag</h1>
<script>
var h1 = document.getElementById('h1');
var x = 'background';
h1.style[x] = "red";
</script>
</body>
</html>
Changing your call to h1.style[x]
sets the property the way you're looking for.
edited Nov 14 '18 at 22:12
answered Nov 14 '18 at 21:50
RyanRyan
3169
3169
So wouldh1.style['background'] = "red";
also be valid? (I assume so)
– GrumpyCrouton
Nov 14 '18 at 21:53
It absolutely would!
– Ryan
Nov 14 '18 at 21:53
That's neat. Though I don't understand whyh1.style.background = "red"
would be a more popular choice given that - I guess maybe the fact it uses 3 symbols less. The other notation just seems more verbose to me
– GrumpyCrouton
Nov 14 '18 at 21:54
@GrumpyCrouton Object Oriented (known notation in most langages)
– Temani Afif
Nov 14 '18 at 21:55
1
@Tarabass I've updated my answer to reflect that - thanks for the suggestion!
– Ryan
Nov 14 '18 at 22:12
|
show 16 more comments
So wouldh1.style['background'] = "red";
also be valid? (I assume so)
– GrumpyCrouton
Nov 14 '18 at 21:53
It absolutely would!
– Ryan
Nov 14 '18 at 21:53
That's neat. Though I don't understand whyh1.style.background = "red"
would be a more popular choice given that - I guess maybe the fact it uses 3 symbols less. The other notation just seems more verbose to me
– GrumpyCrouton
Nov 14 '18 at 21:54
@GrumpyCrouton Object Oriented (known notation in most langages)
– Temani Afif
Nov 14 '18 at 21:55
1
@Tarabass I've updated my answer to reflect that - thanks for the suggestion!
– Ryan
Nov 14 '18 at 22:12
So would
h1.style['background'] = "red";
also be valid? (I assume so)– GrumpyCrouton
Nov 14 '18 at 21:53
So would
h1.style['background'] = "red";
also be valid? (I assume so)– GrumpyCrouton
Nov 14 '18 at 21:53
It absolutely would!
– Ryan
Nov 14 '18 at 21:53
It absolutely would!
– Ryan
Nov 14 '18 at 21:53
That's neat. Though I don't understand why
h1.style.background = "red"
would be a more popular choice given that - I guess maybe the fact it uses 3 symbols less. The other notation just seems more verbose to me– GrumpyCrouton
Nov 14 '18 at 21:54
That's neat. Though I don't understand why
h1.style.background = "red"
would be a more popular choice given that - I guess maybe the fact it uses 3 symbols less. The other notation just seems more verbose to me– GrumpyCrouton
Nov 14 '18 at 21:54
@GrumpyCrouton Object Oriented (known notation in most langages)
– Temani Afif
Nov 14 '18 at 21:55
@GrumpyCrouton Object Oriented (known notation in most langages)
– Temani Afif
Nov 14 '18 at 21:55
1
1
@Tarabass I've updated my answer to reflect that - thanks for the suggestion!
– Ryan
Nov 14 '18 at 22:12
@Tarabass I've updated my answer to reflect that - thanks for the suggestion!
– Ryan
Nov 14 '18 at 22:12
|
show 16 more comments
2
probably because it should be like this
style[x]
– Temani Afif
Nov 14 '18 at 21:49