How to change the css of the button on onclick in angular6? - html

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?

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;
}

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

Related

V-onclick evt.target gets the button inside the span; what should I do?

I have a button with a span inside it that is set to run a function v-on:click. I try to pick up the value1 value attached to the button (naming convention aside) by catching it as an evt.
The problem I'm getting is if I click the side of the button it runs as expected. But if I click the span inside it, I can't pick up the value1 because the evt.target is the span.
I'm converting an existing project to Vue, and this isn't the behavior I expected. What is the best way to deal with this?
Thanks!
<button id="touch-button" class="button float-center" value1="19" v-on:click="emit_values">
<span>19</span>
</button>
emit_values(evt){
$(evt.target).attr("value1")
}
Apparently this is a generic html/javascript issue.
Solution is here: Missing click event for <span> inside <button> element on firefox
I've changed it to target evt.currentTarger, then used css to add the pointer-events: none; styling to all children of those buttons.

How to apply active class to anchor links in a horizontal menu in SCSS

I'm trying to achieve toggle functionality between several anchor links in a horizontal menu. The problem I'm facing currently is, the active class is not being applied to links.
What I was expecting is, when user clicks on the link, the color of the text should change. Only the selected link text color should change.
SCSS
.link-active{
&:active,
&:hover {
color: red;
}
}
HTML
<div class="horizontal-scroll">
<span *ngFor="let menuOption of homePageMenu; let i = index;">
<a class="menu-chip"
[ngClass]="{'link-active': isActive}"
(click)="handleMenuItemClick(i)">{{menuOption.name}}</a>
</span>
</div>
I created a working example using StackBlitz. Could anyone please guide.
I have edited your code in StackBlitz. check it out
https://stackblitz.com/edit/anchor-menu-toggle-9vfa8i
Edited
I have change the followings
in the ts file
isActive = false; to activeIndex = false; to get the current activated index
In the toggle function change to get the clicked index and assign to the activeIndex
in SCSS
&:active to &.active , I used active as a class, not pseudo state
In the html [class.active]="activeIndex==i" added because when activeIndex equels to loop index the active class will add to the element
I hope this will clear every thing.
you could just use a [routerLinkActive]="your-active-class" directive in an anchor tag and it will automatically toggle the classes for you.

Styling an element using inline javascript on onclick

I am trying to style a link. I have an external stylesheet and in the HTML I am pointing to the correct location of the sheet, so that is not the problem. I would like to change the text-decoration when the onclick event is triggered. I have:
<a class="dopelink"onclick="document.getElementsByTagName("link").style.textDecoration="underline" " href="mylink.htm" >MY Link</a>
What am I doing wrong?
<a class="dopelink" style="text-decoration:none" onmousedown="this.style.textDecoration='underline'" href="mylink.htm">My link</a>
Your javascript must use single-quotes because your HTML uses double-quotes.
Set the text-decoration to none since it is underlined by default. This will change when the element is clicked.
Use this instead of document.getElementsByTagName. Since it's inline javascript, it can reference itself.
Use onmousedown instead of onclick.
I hope I understood, if not then I'm sorry :/
Create a :hover option for your html tag... for example I have "a" tag in HTML code like this:
Take me somewhere
and what I like to do is, that the color of this div will be red and when I point this div whit my mouse it will change the color to blue and make underline there, so my code in css will look like this:
a {
color: #F00;
}
a:hover {
color: #00F;
text-decoration: underline;
}
Your code wasn't working because getElementsByTagName returns more then one element (it returns array object properties). You have to loop thought each one of it.
You can check out this fiddle. I set text-decoration to none after click to be more clear.
In test function (which is called onclick) you iterate through every a tag that ElementsByTagName gave you. In this loop you set text-decoration property.
function test(){
var links = document.getElementsByTagName("a");
for (var l = 0; l < links.length; l++){
links[l].style.textDecoration ='none';
}}

Keeping active states in css

I'm creating a log in form and I am doing hide/show states in JavaScript.
Is there a way that when I load the page, the first button is on active state, while when I click anoother button it becomes active and the previous one is no longer active?
My HTML:
<a class="login-button" >login</a><a class="login-button">register</a><a class="login-button">cant login?</a>
My CSS:
.login-button
{color:#00BFFF;
padding-right:20px;
}
.login-button:active
{ color:#FFF;
font-family:customfont-bold;
}
You could just use JavaScript as #Sachin point out, or do it in pure CSS making use of the :target pseudoclass.
The only thing you should do is to add a hyperlink reference (href) to your anchor tag, which, I think, would make your markup more semantic.
HTML
login
CSS
#login:target {
color:#FFF;
font-family:customfont-bold;
}
Since you taget your question just as css/html I thought a CSS solution would be nice.
You can use Javascript or Jquery to achieve this. Here is sample for you
$("a").click(function() {
$("a").removeClass("active");
$(this).addClass("active");
});
CSS
.active
{
color:#FFF;
font-weight:bold;
}
of-course you can use selector more specifically.
Just rename the class that u want to appear active at first to class="login-button active"
Then insert a javascript function to change from one to another, for example:
function{
var element = document.getElementsByClassName("login-button active");
element.className="login-button";
}
You could also add some id="" in order to make easier the changes. Then u can use:
var element = document.getElementById("yourID");

How do you make an anchor link non-clickable or disabled?

I have an anchor link that I want to disable once the user clicks on it. Or, remove the anchor tag from around the text, but definitely keep the text.
<a href='' id='ThisLink'>some text</a>
I can do this easily with a button by adding .attr("disabled", "disabled");
I successfully added the disabled property, but the link was still clickable.
I don't really care if the text is underlined or not.
Any clue?
When you click on the wrong musician, it should just add "Wrong" and then become unclickable.
When you click and you are correct, it should add "Awesome" and then disable all <a> tags.
The cleanest method would be to add a class with pointer-events:none when you want to disable a click. It would function like a normal label.
.disableClick{
pointer-events: none;
}
<a href='javascript:void(0);'>some text</a>
Use pointer-events CSS style. (as Jason MacDonald suggested)
See MDN https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events.
Its supported in most browsers.
Simple adding "disabled" attribute to anchor will do the job if you have global CSS rule like following:
a[disabled], a[disabled]:hover {
pointer-events: none;
color: #e1e1e1;
}
I just realized what you were asking for(I hope). Here's an ugly solution
var preventClick = false;
$('#ThisLink').click(function(e) {
$(this)
.css('cursor', 'default')
.css('text-decoration', 'none')
if (!preventClick) {
$(this).html($(this).html() + ' lalala');
}
preventClick = true;
return false;
});
$('a').removeAttr('href')
or
$('a').click(function(){ return false})
It depends on situation
Bootstrap provide us with .disabled class. Please use it.
But .disabled class only works when the 'a' tag already has class 'btn'. It doesn' t work on any old 'a' tag. The btn class may not be appropriate in some context as it has style connotations. Under the covers, the .disabled class sets pointer-events to none, so you can make CSS to do the same thing as Saroj Aryal and Vitrilo have sugested. (Thank you, Les Nightingill for this advice).
Add a css class:
.disable_a_href{
pointer-events: none;
}
Add this jquery:
$("#ThisLink").addClass("disable_a_href");
The best way is to prevent the default action. In the case of anchor tag, the default behavior is redirecting to href specified address.
So following javascript works best in the situation:
$('#ThisLink').click(function(e)
{
e.preventDefault();
});
You could use the onclick event to disable the click action:
<a href='' id='ThisLink' onclick='return false'>some text</a>
Or you could just use something other than an <a> tag.
Just remove the href attribute from the anchor tag.
Jason MacDonald comments worked for me, tested in Chrome, Mozila and IE.
Added gray color to show disable effect.
.disable_a_href{
pointer-events: none;
**color:#c0c0c0 !important;**
}
Jquery was selecting only first element in the anchor list, added meta character (*) to select and disable all element with id #ThisLink.
$("#ThisLink*").addClass("disable_a_href");
Write this a single line of jQuery Code
$('.hyperlink').css('pointer-events','none');
if you want to write in css file
.hyperlink{
pointer-events: none;
}
Create following class in style sheet :
.ThisLink{
pointer-events: none;
cursor: default;
}
Add this class to you link dynamically as follow.
<a href='' id='elemID'>some text</a>
// or using jquery
<script>
$('#elemID').addClass('ThisLink');
</script>
This is the method I used to disable.Hope it helps.
$("#ThisLink").attr("href","javascript:;");
Try this:
$('a').contents().unwrap();
Simply in SASS:
.some_class{
// styles...
&.active {
pointer-events:none;
}
}
Never trust the browser because the user can change the page in any way without the server's knowledge.
If a link is to work only once, the first thing you need to do is make sure that server side the click is accepted only once (with an onetime token specified as querystring for example), because the URL present in the href attribute can be copied by the user and inserted in the navigation bar of the browser and runned multiple times.
On the javascript side, the safest thing you can do is completely replace the <a> link with another tag, preserving the content:
/** Replace element, preserving attributes and moving descendant nodes from the previous one.
*
* #param {HTMLElement} element Element to be replaced changing tag.
* #param {String} new_tag New element tag.
* #return {HTMLElement} New created element.
*/
function rename_element_tag(element, new_tag) {
let new_block = document.createElement(new_tag);
for (let j = 0; j < element.attributes.length; ++j)
new_block.setAttribute(element.attributes[j].name, element.attributes[j].value);
$(new_block).insertAfter(element);
while (element.childNodes.length > 0)
new_block.appendChild(element.childNodes[0]);
$(element).remove();
return new_block;
}
This function replaces the passed element in place by "modifying" the tag, and preserves attributes and content by iterating all child nodes via vanilla javascript instead of jQuery to handle text nodes as well.
In your case you must skip the href attribute.
$('#ThisLink').one('click',function(){
$(this).bind('click',function(){
return false;
});
});
This would be another way to do this, the handler with return false, which will disable the link, will be added after one click.
The easyest way
In your html:
<a id="foo" disabled="true">xxxxx<a>
In your js:
$('#foo').attr("disabled", false);
If you use it as attribute works perfectly