How to change the css of the button on onclick in angular6?
I have few buttons in my webpage. I need to change the backgroundcolor & font-color when it is clicked. I have tried this,
<div class="card-body">
<h6 class="card-text">Data</h6>
<button ion-button block (click)="viewMore('data');
displayPopup('data');" >
ViewMore</button>
<button ion-button block (click)="display('data');" >Display</button>
</div>
Likewise I have few more divs. Now I need to change the css of the viewmore button when it is clicked.
My css:
button:focus
background-color: blue;
button:active
background-color: red;
Now it is working for second button, when I click the first button popup comes & after closing the popup button css has not changed. Can somebody please suggest me?
html css angular angular6
add a comment |
I have few buttons in my webpage. I need to change the backgroundcolor & font-color when it is clicked. I have tried this,
<div class="card-body">
<h6 class="card-text">Data</h6>
<button ion-button block (click)="viewMore('data');
displayPopup('data');" >
ViewMore</button>
<button ion-button block (click)="display('data');" >Display</button>
</div>
Likewise I have few more divs. Now I need to change the css of the viewmore button when it is clicked.
My css:
button:focus
background-color: blue;
button:active
background-color: red;
Now it is working for second button, when I click the first button popup comes & after closing the popup button css has not changed. Can somebody please suggest me?
html css angular angular6
add your TS code with method displayPopup
– ashiish.me
Nov 11 at 9:06
add class to button onclick and then onclick to close popUp remove the class from button in css set style to this class
– לבני מלכה
Nov 11 at 9:11
2
you can use ngStyle or ngClass
– Joel Joseph
Nov 11 at 9:18
You can't use ngStyle with pseudoclasses.
– Christopher Dosin
Nov 11 at 9:46
add a comment |
I have few buttons in my webpage. I need to change the backgroundcolor & font-color when it is clicked. I have tried this,
<div class="card-body">
<h6 class="card-text">Data</h6>
<button ion-button block (click)="viewMore('data');
displayPopup('data');" >
ViewMore</button>
<button ion-button block (click)="display('data');" >Display</button>
</div>
Likewise I have few more divs. Now I need to change the css of the viewmore button when it is clicked.
My css:
button:focus
background-color: blue;
button:active
background-color: red;
Now it is working for second button, when I click the first button popup comes & after closing the popup button css has not changed. Can somebody please suggest me?
html css angular angular6
I have few buttons in my webpage. I need to change the backgroundcolor & font-color when it is clicked. I have tried this,
<div class="card-body">
<h6 class="card-text">Data</h6>
<button ion-button block (click)="viewMore('data');
displayPopup('data');" >
ViewMore</button>
<button ion-button block (click)="display('data');" >Display</button>
</div>
Likewise I have few more divs. Now I need to change the css of the viewmore button when it is clicked.
My css:
button:focus
background-color: blue;
button:active
background-color: red;
Now it is working for second button, when I click the first button popup comes & after closing the popup button css has not changed. Can somebody please suggest me?
html css angular angular6
html css angular angular6
edited Nov 11 at 10:41
asked Nov 11 at 9:02
Priyanka
13
13
add your TS code with method displayPopup
– ashiish.me
Nov 11 at 9:06
add class to button onclick and then onclick to close popUp remove the class from button in css set style to this class
– לבני מלכה
Nov 11 at 9:11
2
you can use ngStyle or ngClass
– Joel Joseph
Nov 11 at 9:18
You can't use ngStyle with pseudoclasses.
– Christopher Dosin
Nov 11 at 9:46
add a comment |
add your TS code with method displayPopup
– ashiish.me
Nov 11 at 9:06
add class to button onclick and then onclick to close popUp remove the class from button in css set style to this class
– לבני מלכה
Nov 11 at 9:11
2
you can use ngStyle or ngClass
– Joel Joseph
Nov 11 at 9:18
You can't use ngStyle with pseudoclasses.
– Christopher Dosin
Nov 11 at 9:46
add your TS code with method displayPopup
– ashiish.me
Nov 11 at 9:06
add your TS code with method displayPopup
– ashiish.me
Nov 11 at 9:06
add class to button onclick and then onclick to close popUp remove the class from button in css set style to this class
– לבני מלכה
Nov 11 at 9:11
add class to button onclick and then onclick to close popUp remove the class from button in css set style to this class
– לבני מלכה
Nov 11 at 9:11
2
2
you can use ngStyle or ngClass
– Joel Joseph
Nov 11 at 9:18
you can use ngStyle or ngClass
– Joel Joseph
Nov 11 at 9:18
You can't use ngStyle with pseudoclasses.
– Christopher Dosin
Nov 11 at 9:46
You can't use ngStyle with pseudoclasses.
– Christopher Dosin
Nov 11 at 9:46
add a comment |
2 Answers
2
active
oldest
votes
The easiest way would be to use CSS pseudoclasses like you did.
For clicked - or visited - elements, there's the :visited
pseudoclass.
You should be familiar with basic css. W3schools is an awesome resource.
So in your case:
button:visited
background: red;
:focus
is - as the same says - for the element which has focus.
Or:
button:active,
button:visited,
button:focus
background: red;
Thank you for the example. But still after closing the popup button css is not changing. 2nd button is working fine.
– Priyanka
Nov 11 at 10:11
Ah, of course. Because you have a<button>
within a<button>
. That's not valid HTML. And your first button has the attributionbutton
the second hasion-button
. So i assume you have to change the attribut toion-button
for the first button.
– Christopher Dosin
Nov 11 at 10:20
Sorry that was a Typo. I dont have button withinn button. those are separate buttons.
– Priyanka
Nov 11 at 10:40
add a comment |
Your question it's not clear. The way to use ngStyle or ngClass otr style.attrib is used a variable and ternary operator. As you has severals buttons you need several variables. this variables can be a property of an object or an array. As an array
//You have an array
buttonsClicked:boolean;
//In your .html
<!--use index 0 for the first button, index 1 for the second button... -->
<button ionbutton block [style.background-color]="buttonsClicked[0]?'red':'green'"
(click)="buttonsClicked[0]=true;
viewMore('data');displayPopup('data');" >
ViewMore</button>
....
</div>
//when you close the popup you can clear all the buttonsClicked
this.buttonsClicked= //<--reinit the array
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247231%2fhow-to-change-the-css-of-the-button-on-onclick-in-angular6%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The easiest way would be to use CSS pseudoclasses like you did.
For clicked - or visited - elements, there's the :visited
pseudoclass.
You should be familiar with basic css. W3schools is an awesome resource.
So in your case:
button:visited
background: red;
:focus
is - as the same says - for the element which has focus.
Or:
button:active,
button:visited,
button:focus
background: red;
Thank you for the example. But still after closing the popup button css is not changing. 2nd button is working fine.
– Priyanka
Nov 11 at 10:11
Ah, of course. Because you have a<button>
within a<button>
. That's not valid HTML. And your first button has the attributionbutton
the second hasion-button
. So i assume you have to change the attribut toion-button
for the first button.
– Christopher Dosin
Nov 11 at 10:20
Sorry that was a Typo. I dont have button withinn button. those are separate buttons.
– Priyanka
Nov 11 at 10:40
add a comment |
The easiest way would be to use CSS pseudoclasses like you did.
For clicked - or visited - elements, there's the :visited
pseudoclass.
You should be familiar with basic css. W3schools is an awesome resource.
So in your case:
button:visited
background: red;
:focus
is - as the same says - for the element which has focus.
Or:
button:active,
button:visited,
button:focus
background: red;
Thank you for the example. But still after closing the popup button css is not changing. 2nd button is working fine.
– Priyanka
Nov 11 at 10:11
Ah, of course. Because you have a<button>
within a<button>
. That's not valid HTML. And your first button has the attributionbutton
the second hasion-button
. So i assume you have to change the attribut toion-button
for the first button.
– Christopher Dosin
Nov 11 at 10:20
Sorry that was a Typo. I dont have button withinn button. those are separate buttons.
– Priyanka
Nov 11 at 10:40
add a comment |
The easiest way would be to use CSS pseudoclasses like you did.
For clicked - or visited - elements, there's the :visited
pseudoclass.
You should be familiar with basic css. W3schools is an awesome resource.
So in your case:
button:visited
background: red;
:focus
is - as the same says - for the element which has focus.
Or:
button:active,
button:visited,
button:focus
background: red;
The easiest way would be to use CSS pseudoclasses like you did.
For clicked - or visited - elements, there's the :visited
pseudoclass.
You should be familiar with basic css. W3schools is an awesome resource.
So in your case:
button:visited
background: red;
:focus
is - as the same says - for the element which has focus.
Or:
button:active,
button:visited,
button:focus
background: red;
answered Nov 11 at 9:48
Christopher Dosin
1687
1687
Thank you for the example. But still after closing the popup button css is not changing. 2nd button is working fine.
– Priyanka
Nov 11 at 10:11
Ah, of course. Because you have a<button>
within a<button>
. That's not valid HTML. And your first button has the attributionbutton
the second hasion-button
. So i assume you have to change the attribut toion-button
for the first button.
– Christopher Dosin
Nov 11 at 10:20
Sorry that was a Typo. I dont have button withinn button. those are separate buttons.
– Priyanka
Nov 11 at 10:40
add a comment |
Thank you for the example. But still after closing the popup button css is not changing. 2nd button is working fine.
– Priyanka
Nov 11 at 10:11
Ah, of course. Because you have a<button>
within a<button>
. That's not valid HTML. And your first button has the attributionbutton
the second hasion-button
. So i assume you have to change the attribut toion-button
for the first button.
– Christopher Dosin
Nov 11 at 10:20
Sorry that was a Typo. I dont have button withinn button. those are separate buttons.
– Priyanka
Nov 11 at 10:40
Thank you for the example. But still after closing the popup button css is not changing. 2nd button is working fine.
– Priyanka
Nov 11 at 10:11
Thank you for the example. But still after closing the popup button css is not changing. 2nd button is working fine.
– Priyanka
Nov 11 at 10:11
Ah, of course. Because you have a
<button>
within a <button>
. That's not valid HTML. And your first button has the attribut ionbutton
the second has ion-button
. So i assume you have to change the attribut to ion-button
for the first button.– Christopher Dosin
Nov 11 at 10:20
Ah, of course. Because you have a
<button>
within a <button>
. That's not valid HTML. And your first button has the attribut ionbutton
the second has ion-button
. So i assume you have to change the attribut to ion-button
for the first button.– Christopher Dosin
Nov 11 at 10:20
Sorry that was a Typo. I dont have button withinn button. those are separate buttons.
– Priyanka
Nov 11 at 10:40
Sorry that was a Typo. I dont have button withinn button. those are separate buttons.
– Priyanka
Nov 11 at 10:40
add a comment |
Your question it's not clear. The way to use ngStyle or ngClass otr style.attrib is used a variable and ternary operator. As you has severals buttons you need several variables. this variables can be a property of an object or an array. As an array
//You have an array
buttonsClicked:boolean;
//In your .html
<!--use index 0 for the first button, index 1 for the second button... -->
<button ionbutton block [style.background-color]="buttonsClicked[0]?'red':'green'"
(click)="buttonsClicked[0]=true;
viewMore('data');displayPopup('data');" >
ViewMore</button>
....
</div>
//when you close the popup you can clear all the buttonsClicked
this.buttonsClicked= //<--reinit the array
add a comment |
Your question it's not clear. The way to use ngStyle or ngClass otr style.attrib is used a variable and ternary operator. As you has severals buttons you need several variables. this variables can be a property of an object or an array. As an array
//You have an array
buttonsClicked:boolean;
//In your .html
<!--use index 0 for the first button, index 1 for the second button... -->
<button ionbutton block [style.background-color]="buttonsClicked[0]?'red':'green'"
(click)="buttonsClicked[0]=true;
viewMore('data');displayPopup('data');" >
ViewMore</button>
....
</div>
//when you close the popup you can clear all the buttonsClicked
this.buttonsClicked= //<--reinit the array
add a comment |
Your question it's not clear. The way to use ngStyle or ngClass otr style.attrib is used a variable and ternary operator. As you has severals buttons you need several variables. this variables can be a property of an object or an array. As an array
//You have an array
buttonsClicked:boolean;
//In your .html
<!--use index 0 for the first button, index 1 for the second button... -->
<button ionbutton block [style.background-color]="buttonsClicked[0]?'red':'green'"
(click)="buttonsClicked[0]=true;
viewMore('data');displayPopup('data');" >
ViewMore</button>
....
</div>
//when you close the popup you can clear all the buttonsClicked
this.buttonsClicked= //<--reinit the array
Your question it's not clear. The way to use ngStyle or ngClass otr style.attrib is used a variable and ternary operator. As you has severals buttons you need several variables. this variables can be a property of an object or an array. As an array
//You have an array
buttonsClicked:boolean;
//In your .html
<!--use index 0 for the first button, index 1 for the second button... -->
<button ionbutton block [style.background-color]="buttonsClicked[0]?'red':'green'"
(click)="buttonsClicked[0]=true;
viewMore('data');displayPopup('data');" >
ViewMore</button>
....
</div>
//when you close the popup you can clear all the buttonsClicked
this.buttonsClicked= //<--reinit the array
answered Nov 11 at 10:46
Eliseo
5,1881311
5,1881311
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247231%2fhow-to-change-the-css-of-the-button-on-onclick-in-angular6%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
add your TS code with method displayPopup
– ashiish.me
Nov 11 at 9:06
add class to button onclick and then onclick to close popUp remove the class from button in css set style to this class
– לבני מלכה
Nov 11 at 9:11
2
you can use ngStyle or ngClass
– Joel Joseph
Nov 11 at 9:18
You can't use ngStyle with pseudoclasses.
– Christopher Dosin
Nov 11 at 9:46