I'm working on a rating stars field for a little form, but I can't change the HTML structure because it's created using Gravity Forms, so I just can "manipulate" it with CSS.
The form is structured as follows:
<ul>
<li>
<input type="radio" value="5">
<label>5/label>
</li>
<li>
<input type="radio" value="4">
<label>4</label>
</li>
<li>
...
</li>
</ul>
I've been using this example but now, with this structure, the [input/label] elements are not siblings of the other [input/label] elements.
How can I make it work without modifying the markup?
Thanks!
If you have to use that html structure, you can't do it without using javascript. I'm assuming you cant edit the current elements either (if you can, do so) so i have including that in the javascript as well, adding attributes as required.
http://codepen.io/partypete25/pen/BjyegJ
Here I've wrapped your list in a div with class .rating
Then i update the inputs and labels with the required attributes to emulate the example you have in your comment. Then i use jquerys replaceWith function to remove the list structure and return only the inputs and labels, wrapped in the .ratings div.
$(".rating ul li").each(function(i){
$("input", this).attr({
"id" : "star"+parseInt(5-i),
"name" : "rating"
});
$("label", this).attr({
"class": "full",
"for" : "star"+parseInt(5-i),
"title" : "Awesome - "+parseInt(5-i)+" stars"
});
});
replaceIt();
function replaceIt() {
$(".rating ul").replaceWith(function() {
return $('input, label', this);
});
}
Related
I have two html pages page_1.html and page_2.html. In page_1.html, I have a button, which upon being clicked should redirect to page_2.html. But it should redirect only when the button has a charteuse background color.
So, in page_1.html, I have a button:
Organization:<div id="org"><input type="checkbox" id="cb1" >ID no: <input type="number" id="org_number" style="visibility: hidden"><br><br>
<input type="checkbox" id="cb2" >Mobile No: <input type="tel" id="ph_number" style="visibility: hidden" required></div><br><br>
<button id="button" onmouseover="hovar()" onclick="submit()" >Register</button>
<script src="back_end.js" async></script>
My javascript (back_end.js):
function hovar(){
var phone=document.getElementById("ph_number").value;
var btn=document.getElementById("button");
if (phone.length!=10){
btn.style.backgroundColor="lightsalmon"
}
else{
btn.style.backgroundColor="chartreuse"
btn.style.color="black"
}
}
function submit(){
var btn=document.getElementById("button");
if (getComputedStyle(btn).backgroundColor == "charteuse"){
window.location.href="page_2.html";
}
}
But, it doesn't redirect to page_2.html. What am I missing here? I have also tried window.location.replace("page_2.html"), but it's the same.
EDIT: I have changed the code a little, it's from a project I'm doing. I have also tried getComputedStyle(document.getElementById("button")).backgroundColor, but it doesn't work.
Another thing that I've noticed, is that when I use:
if (btn.style.backgroundColor == "charteuse"){
//console.log(true)
location.href="page_2.html";
}
it prints true into the console but still doesn't redirect to page_2.html.
But if I use:
if (getComputedStyle(btn).backgroundColor == "charteuse"){
//console.log(true)
window.location.href="page_2.html";
}
it doesn't print true into the console.
But nevertheless, in both the cases, it doesn't redirect to page_2.html
ElementCSSInlineStyle.style
The style property is used to get as well as set the inline style of
an element. When getting, it returns a CSSStyleDeclaration object that
contains a list of all styles properties for that element with values
assigned for the attributes that are defined in the element's inline
style attribute.
https://developer.mozilla.org/en-US/docs/Web/API/ElementCSSInlineStyle/style
So your if-conditon document.getElementById("button").style.backgoundColor == "red" does never return true because the color is defined in your css-file and not as an inline argument.
A solution would be using getComputedStyle(element) which returns the actuall style from the css-file.
getComputedStyle(document.getElementById("button")).backgroundColor == "red"
as explained here https://zellwk.com/blog/css-values-in-js/
Also in your css, you can remove the quotationmarks around "red" as mentioned by #George
The styles property doesn't directly reflect your CSS, so running
if(document.getElementById("button").style.backgoundColor=="red"){
never works.
What you can do is change the color to red using javascript:
function changeButtonColor(color) {
document.getElementById("button").style.backgoundColor = color;
}
changeButtonColor('red');
So you do this, wherever you need to change the background color, your if statement will work correctly and you can switch.
so you should compare like this
var btn=document.getElementById("button");
if (getComputedStyle(btn).backgroundColor == "rgb(127, 255, 0)"){
window.location.href="page_2.html";
}
}
Is it possible to style the value in the attribute ngModel of an input tag?
Example:
<input class="input" type="text" [(ngModel)] = "myService.text">
Let's say the value of text is '28 packages', can I put 28 in bold?
So if i understand correctly you want to have it bold whenever the value is 28 ?
yes its possible you can use a ng-class with a ternary expression like this
.bold{
font-weight:600;
}
<input type="text" ng-class="myService.text == '28 ? 'bold' : '''" class="input" ng-model="myService.text" />
This is not angular-related rather a CSS related question.
You cannot style only a part of an input in HTML/CSS so you won't be able to do it in angular.
Instead, you can use an input that is hidden behind a div. The idea is that when the user clicks the div, you actually focus the input. When the user types text, you capture the content of the input and fill the div with it, eventually adding <span class"highlight"> around the number of packages.
I prepared you a stackblitz in pure CSS/JS. You can adapt it in angular if you want.
Relevant pieces of code :
HTML :
<span id="hiddenSpan">This is the hidden div. Click it and start typing</span>
<div>
<label for="in">The real input</label>
<input id="in" type="text">
</div>
JS :
const input = document.getElementById('in')
const hiddenSpan = document.getElementById('hiddenSpan')
function onInputChanged() {
let text = input.value
const regex = new RegExp('(\\d+) packages')
let result = regex.exec(text)
if(result) {
hiddenSpan.innerHTML = '<span class="highlight">'+result[1]+'</span> packages'
} else {
hiddenSpan.innerHTML = text
}
}
// Capture keystrokes.
input.addEventListener('keyup', onInputChanged)
// Focus the input when the user clicks the pink div.
hiddenSpan.addEventListener('click', function() {
input.focus()
})
CSS :
#hiddenSpan {
background-color: pink;
}
.highlight {
font-weight: bold;
background-color: greenyellow;
}
Note : the downside is that the blinking caret is not visible anymore. You can take a look at this resource if you want to simulate one.
It is not possible to style certain parts of a text <input> field in bold. However, you can use a contenteditable div instead of a text <input> field. Inside the contenteditable div you can have other HTML tags like <strong> to style certain parts of the text however you like.
I created an Angular directive called contenteditableModel (check out the StackBlitz demo here) and you can use it to perform 2-way binding on a contenteditable element like this:
<div class="input" contenteditable [(contenteditableModel)]="myService.text"></div>
The directive uses regular expressions to automatically check for numbers in the inputted text, and surrounds them in a <strong> tag to make them bold. For example, if you input "28 packages", the innerHTML of the div will be formatted like this (to make "28" bolded):
<strong>28</strong> packages
This is the code used in the directive to perform the formatting:
var inputElement = this.elementRef.nativeElement;
inputElement.innerHTML = inputElement.textContent.replace(/(\d+)/g, "<strong>$1</strong>");
this.change.emit(inputElement.textContent);
You can change the <strong> tag to something else (e.g. <span style="text-decoration: underline"> if you want the text to be underlined instead of bolded).
When performing the formatting, there is an issue where the user's text cursor position will be unexpectedly reset back to the beginning of the contenteditable div. To fix this, I used 2 functions (getOriginalCaretPosition and restoreCaretPosition) to store the user's original cursor position and then restore the position back after the text formatting is performed. These 2 functions are kind of complex and they're not entirely relevant to the OP's question so I will not go into much detail about them here. You can PM me if you want to learn more about them.
Everytime a checkbox inside my template is selected, I want to add a class to my polymer element or change polymer element's attribute.
Since my element uses label for attribute, I bound the ID of the checkbox to when instantiating the element in my markup
<label for="{{uniqueid}}" on-tap="{{toggle}}" />
<input type="checkbox" id="{{uniqueid}}" class="select" checked="{{checker}}">
<img src="{{image}}" alt="" class="prod-img">
<div class="grid-details">
<span>{{designer}}</span>
<span>{{product}}</span>
<span>{{currency}} {{price}}</span>
<span>Shipped from {{info}}</span>
</div>
</label>
I would then call the element like so
<gmselect-element uniqueid="four" image="http://i.imgur.com/fkq1QKq.jpg" designer="CUCARELIQUIA" product="Castellano Suede Bag Redder" info="Gijon, Spain" price="650" currency="$"></gmselect-element>
My toggle function looks like below
toggle: function() {
this.$.grid.setAttribute("class", "grid true");
this.setAttribute("selected", "true");
}
However, instead of setting it to true here, I would like to check the value of the checkbox's checked property. Since ID is not static, I can't get element using the $ functionality. I also don't know how to escape a data bound in moustache's to get its value inside a method.
this.$.grid.classList.add('classname')
this.$.grid.classList.remove('classname')
You've bound the checked value to {{checker}}, so you should be able to just refer to this.checker in your toggle function.
If you need to, you can also get the model data from the event. See:
https://www.polymer-project.org/1.0/docs/devguide/data-binding
I'm trying to get CSS Switches to work with ASP.NET rendered check-box.
The way that CSS Switcher structured and the ASP.NET check-box rendered make it hard to me to get it work and the problem not only in the span generated around the input it's also the label - which I need for sure - for the check-box, once I remove them from rendered HTML it works.
ASP.NET Check-box adds span and label when rendered as follow:
<span>
<input id="ID" type="checkbox" checked="checked" name="ID"></input>
<label for="ID">Label</label>
</span>
While the switcher needs the input to be wrapped in the following form:
<label class="switch switch-default">
<input type="checkbox" checked><span></span>
</label>
Here is an Example
here is how my check-box coded in my ASP HTML
<div class="col-sm-9">
<label class="switch switch-default"><asp:CheckBox id="chkAdd" runat="server" Text="Add" CssClass=""></asp:CheckBox><span></span></label>
</div>
Is there any workaround this problem?
I have found a solution and wanted to share.
As appears in this jsFiddle example which uses code similar to ASP.NET rendered check-boxes, the problem was with the CSS selectors and how to reach a parent that has a child check-box with checked or unchecked .. that is not applicable still in CSS. I had to add a jQuery function and change the selectors in my CSS to reach this jsFiddle result
$(document).ready(function () {
$('.switch input').each(function () {
var _span = $(this).closest('.switch').children('span.sw-inner');
if ($(this).is(":checked")) {
$(_span).addClass('checked').removeClass('un-checked');
} else {
$(_span).addClass('un-checked').removeClass('checked');
}
});
$('.switch input').change(function () {
var _span = $(this).closest('.switch').children('span.sw-inner');
if ($(this).is(":checked")) {
$(_span).addClass('checked').removeClass('un-checked');
} else {
$(_span).addClass('un-checked').removeClass('checked');
}
});
});
I have multiple labels on a bar my web page and I want when I hover on that label, image in other div should change based on the label being hovered.
Any help would be highly appreciared.
Sjain
With jQuery, the popular javascript library, you could do it rather easily:
-- Sample HTML
<label class="target">Hover to Change</label>
<img src="image001.gif" class="sourceImage" />
-- Corresponding jQuery
$("label.target").hover(
function () {
// mousing-over <label class="target"> changes img to 'image002.gif'
$("img.sourceImage").attr("src", "image002.gif");
},
function () {
// mousing-out <label class="target"> changes img to 'image001.gif'
$("img.sourceImage").attr("src", "image001.gif");
}
);