/    Sign up×
Community /Pin to ProfileBookmark

Adding multiple styles with SavaScript

I have this code that adds 0.4 opacity to an clicked element, but I want do add a border also. How I can do it? I also want the border to be on the inside.

[code]
const opacity = 0.4;
this.style.opacity = opacity;[/code]

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@VITSUSAJun 26.2019 — Can you please give me an example or share the snapshot for better understanding?
Copy linkTweet thisAlerts:
@codyhillauthorJun 26.2019 — @VITSUSA#1605400

When the image is active I have the opacity to 0.4 but I also want the img to have a border on the inside so it does not mess with the margins and push the others imgs around

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-06-26/1561543883-421508-dfvfdf.png]
Copy linkTweet thisAlerts:
@cootheadJun 26.2019 — Hi there RaulRogojan,

try it like this...

``<i>
</i> this.style.opacity = opacity;
this.style.border = '1px solid #000';
this.style.boxSizing = 'border-box'; <i>
</i>
``

_coothead_
Copy linkTweet thisAlerts:
@codyhillauthorJun 26.2019 — @coothead#1605411 It's still on the out side but nvm is messing with some other code and I don't know how to fix it
Copy linkTweet thisAlerts:
@codyhillauthorJun 26.2019 — @coothead#1605411 NVM , I fiex the issues with the other codes but the border is still on the outside :D . But the border is still on the outside, I am trying with inset but I don't know how to write that in JS
Copy linkTweet thisAlerts:
@cootheadJun 26.2019 — > @RaulRogojan#1605413

> **[color=#069] It's still on the out side...[/color]**


Here is my test code, which show that it works correctly...

``<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;untitled document&lt;/title&gt;

&lt;!--&lt;link rel="stylesheet" href="screen.css" media="screen" --&gt;

&lt;style media="screen"&gt;
body {
background-color: #f9f9f9;
font: 100% / 162% verdana, arial, helvetica, sans-serif;
}
#test {
display: block;
max-width: 30em;
height: auto;
margin: auto;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;img id="test" src="https://coothead.co.uk/images/blood.jpg" width="768" height="512" alt="blood"&gt;

&lt;script&gt;
(function( d ) {
'use strict';
d.getElementById( 'test' ).addEventListener( 'click',
function() {
this.style.opacity = 0.4;
this.style.border = '1px solid #000';
this.style.boxSizing = 'border-box';
}, false );
}( document ));
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;<i>
</i>
`</CODE>

<EM>_coothead_</EM>


<CODE>
``
Copy linkTweet thisAlerts:
@cootheadJun 26.2019 — Hi there RaulRogojan,

now that I have answered your question, I will show you a method which

is preferable because it removes the inline styling from the JavaScript...

``<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;untitled document&lt;/title&gt;

&lt;!--&lt;link rel="stylesheet" href="screen.css" media="screen" --&gt;

&lt;style media="screen"&gt;
body {
background-color: #f9f9f9;
font: 100% / 162% verdana, arial, helvetica, sans-serif;
}
#test {
display: block;
max-width: 30em;
height: auto;
margin: auto;
box-sizing: border-box;
}
.extras {
opacity: 0.4;
border: 1px solid #000;
}

&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;img id="test" src="https://coothead.co.uk/images/blood.jpg" width="768" height="512" alt="blood"&gt;

&lt;script&gt;
(function( d ) {
'use strict';
var test = true;
d.getElementById( 'test' ).addEventListener( 'click',
function() {
if ( test === true ) {
this.classList.add( 'extras' );
test = false;
}
else {
this.classList.remove( 'extras' );
test = true;
}
}, false );
}( document ));
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;<i>
</i>
``


_coothead_
Copy linkTweet thisAlerts:
@codyhillauthorJun 26.2019 — @coothead#1605415 Here is mine

https://github.com/CodyHiII/Second-Portfolio
Copy linkTweet thisAlerts:
@cootheadJun 26.2019 — Hi there RaulRogojan,

does this help...

**Javascript :-**
``<i>
</i>
// Logos galery -----

const logosMain = document.querySelector('.logos__main');
const thumbs = document.querySelectorAll('.imgs img');

thumbs.forEach( img =&gt; img.addEventListener('click', imgClicked));

function imgClicked(e) {
if ( window.getComputedStyle(this).getPropertyValue('opacity') == 1 ) {
this.classList.add( 'extras' );
}
else {
this.classList.remove( 'extras' );
}
}<i>
</i>
`</CODE>

<STRONG>**CSS :-**</STRONG>

<CODE>
`<i>
</i>.imgs img {
display: block;
width: 150px;
height: auto;
}
.extras {
border: 5px solid #000;
opacity: 0.4;
}<i>
</i>
``


_coothead_
Copy linkTweet thisAlerts:
@codyhillauthorJun 26.2019 — @coothead#1605427 It does not , But I'll try thinking of something else to make the selected img more visible that will be easier to execute
×

Success!

Help @codyhill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...