jquery change style class attribute value - html

I have style css cur_font class
I want to change font-family to 'arial' by jquery
and also new font affected on class selectors html,body,h2,h1,div,td
<style>
.cur_font html,body,h2,h1,div,td { font-family:'tahoma'; }
</style>
<script>
$(".change_font").on("click", function (e) {
var font='arial';
$("style.cur_font").css("font-family",font);
});
</script>
trying to change font-family attribute for class cur_font using jquery

The easiest way to do this would be to toggle a class on the body and apply different styles.
Another approach would be to loop over document.styleSheets and then loop over the cssRules and check to see if the selectorText matches. If it does, modify the rule.
Other option is to use a CSS variable and alter the value of the variable.
document.querySelector(".buttons").addEventListener("click", evt => {
const btn = evt.target.closest("button");
if (!btn) return;
const color = btn.dataset.color;
document.documentElement.style.setProperty('--myVariable', color);
});
:root {
--myVariable: red;
}
div {
background-color: var(--myVariable);
}
<div>Hello World</div>
<div class="buttons">
<button data-color="blue">blue</button>
<button data-color="red">red</button>
<button data-color="yellow">yellow</button>
</div>

Related

How to change CSS that Django template uses with a button?

I am trying to implement accessibility option on my page that would change CSS to different file when accessibility button would be clicked.
For now, all my templates extends base_generic.html, where style.css is loaded. When accessibility button would be clicked, I wish for it to change to use style_access.css for that user. How can I accomplish that?
I think a way could be, to refer in the HTML template to both CSS files, and use an onclick function with javascript, and jquery to change the id or class of the specific elements of the template.
So for example,
let's say I wanted onclick to change the CSS of an element, I could make a counter and toggle between two ids that I will have referenced in my CSS file or files.
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<button>This is a div</button>
<h1 class="potatoe" id="hello">HELLO THIS IS TEXT</h1>
<style>
#hello { color: red; }
#bye { color: blue; }
</style>
<script>
var clickCount = 0;
$("button").on("click", function() {
clickCount++;
$(".potatoe").attr("id", clickCount % 2 === 0 ? "bye" : "hello");
});
</script>
</body>
As you'll see everytime you click the button the CSS of the element will change
This is not exactly changing between CSS files but it ultimately changes the CSS of the elements you want to select.
You can implement by using JavaScript more easily:
const toggleButton = document.getElementById('button');
const workContainer = document.getElementById('work');
toggleButton.addEventListener('click', () => {
document.body.classList.toggle('blue');
toggleButton.classList.toggle('active');
workContainer.classList.toggle('blue');
if(document.body.classList.contains('blue')){
localStorage.setItem('blue', 'enabled');
}else{
localStorage.setItem('blue', 'disabled');
}
});
if(localStorage.getItem('blue') == 'enabled'){
document.body.classList.toggle('blue');
toggleButton.classList.toggle('active');
workContainer.classList.toggle('blue');
}

How to change the button color using ng-click function?

I got the buttons list from resource. It consist of 10 buttons like
t1,t2,t3,t4,t5,t6,t7,t8,t9,t10
When I click the on t1 button first time button color change to red. Second time I click on same button color change to green.
Please tell me how to do this
<div class="col-sm-2 col-lg-1 col-md-1" ng-repeat="table in tables" style="margin-left:1px">
<button type="button" class="btn btn-success" ng-click="getTable(table)">{{table.tablename}}</button>
</div>
this is my method
$scope.ng-click=gettable(table){
//
}
It's easy. Just bind one more property with your all the items in tables array like this-
for(var i = 0; i < $scope.tables.length; i++)
{
$scope.tables[i].btnClass = "btn-success";
}
then in HTML, assign this class like this:
<button type="button" class="btn {{table.btnClass}}" ng-click="getTable(table)">
{{table.tablename}}</button>
and ng-click function as:
$scope.getTable = function(table) {
table.btnClass = table.btnClass == "btn-info" ? "btn-success" : "btn-info"
}
Or if you want to change manual colors then create one class in css like this:
.red-button {
background-color: "red";
}
Then in the function
$scope.getTable = function(table) {
table.btnClass = table.btnClass == "red-button" ? "btn-success" : "red-button"
}
Here's my suggestion: Inside your $scope.ng-click=gettable(table){ ...... }
Create an array of colors
Everytime the button clicks, pull a random color value inside that array
Using the controllerAs syntax (which I advise you to use), you can work with the following.
html:
<div ng-repeat="table in ctrl.tables">
<button type="button" ng-click="ctrl.getTable(table)" ng-style="{ 'background-color': table.color }">{{table.tablename}}</button>
</div>
js:
this.getTable = function(table) {
table.color = "red";
};
https://plnkr.co/edit/axzyUQJowlSEClszrIQs
var colors=['red','green','yellow','black','blue'];
$scope.color=null;
$scope.ng-click=gettable(table){
var colorToApply=colors[Math.floor(Math.random()*colors.length)];
$scope.color=colorToApply
}
Now you can define css rule here
.green{
color:green;
}
.blue{
color:blue;
}
.red{
color:red;
}
.yellow{
color:yellow;
}
Then in your view you can set ng-style on the button you want to change color and bind it to color in the scope like
<button ng-style="color">Change color</button>

Dynamic Inline CSS using AngularJS

I have an angular app and need to apply some CSS styles on a page, at runtime.
Solution using ng-style is not scalable:
I am aware that for specific (known) items, this can easily be done using the ng-style directive e.g.:
<div id="mydiv" ng-style="{color: bgColor}">ABCD</div>
However, this technique cannot be applied to all the <a> or <p> tags on the page. How does one apply a dynamic style based on an angular scope variable to ALL instances of a tag on the page?
Something like the following would be ideal:
<style>
.in3_counter {color: {{settings.in3Color}};}
.in4_counter {color: {{settings.in4Color}};}
</style>
Update: The value of the css scope variables isn't predetermined, so I we don't know what colors would be applied to the elements as the variables are set at runtime (e.g. with color picker).
Any suggestions?
Check the following example:
var COLOR_CTRL = function($scope, $sce) {
$scope.changeColor = function(color) {
$scope.style = $sce.trustAsHtml('a, p {color: ' + color + '}');
};
$scope.changeColor('#000');
};
var app = angular.module('app', []);
app.controller('ColorCtrl', ['$scope', '$sce', COLOR_CTRL]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script>
<div ng-controller="ColorCtrl" ng-app="app">
anchor
<p>paragraph</p>
<div>
<button ng-click="changeColor('#f00')">red</button>
<button ng-click="changeColor('#0f0')">green</button>
<button ng-click="changeColor('#00f')">blue</button>
</div>
<style data-ng-bind-html="style"></style>
</div>

knockoutjs css binding value when boolean computed property is false/true

I am binding my currently selected item like this:
The selected item gets visible, but how can I give the unselected items an unselected/default background color? When I set a background color to the template class I do not see anymore the background color set from the selection via mouse click.
Is the problem maybe that I have 2 backgrounds set but none is removed?
<div data-bind="foreach: items">
<div class="cellContainer" >
<div class="template" data-bind="click: $parent.selectItem, css: { selected: isSelected }">
<div data-bind="text: number"></div>
</div>
</div>
</div>
.selected {
background-color: #0094ff;
}
Sounds like a cascade issue. Make sure your ".template" style is defined before your ".selected" style in your css file.
Make sure your selectItem method resets isSelected on all elements before setting it to true on the argument. A naive implementation could be:
var ViewModel = function() {
// Skipped the rest for brevity...
self.selectItem = function(item) {
// Deselect all items first
for (var i = 0; i < self.items().length; i++) {
self.items()[i].isSelected(false);
}
// Select the argument
item.isSelected(true);
};
};
See this jsfiddle for a demo.
However, it is often easier to keep a reference on the parent view model to the currently selected item, and change the css binding to something like:
css: { selected: $parent.TheOneSelectedItem().number() == number() }
See this fiddle for the alternate demo.
Something like this might work.
.template {
background-color: #fff;
}
.template.selected {
background-color: #0094ff;
}
It does not look like a knockout issue.

CSS selector in the URL

When you have a url like this www.example.com/#signup, what the browser does is just to focus it's view on the HTML element with id signup, is that right?
Is it possible to change the element's CSS style if it's focused in that way?
E.g. let's say I have a div element, with the id signup, then if I enter www.example.com, the div's background is white and if I enter www.example.com/#signup it's yellow. Like "emphasizing" the sign up form. Is this at all possible?
:target { background-color: yellow; }
The :target psuedo-class does exactly this.
You can read the window.location.hash property when the page loads and apply the corresponding css classs. Something like:
<script type='text/javascript>
function init(){
var hash = window.location.hash.substr(1);
var element = document.getElementById(hash);
if(element){
element.className += " emphasize";
}
}
</script>
<body onload="init()">
...
</body>