Indesign CS6, Javascript - javascript

I am a javascript beginner. I am working in indesign CS6. I have assigned paragraph and character styles to all the fonts being used throughout my document. Using character styles I have changed the color of some type to be purple. The purple text is being used to call specific attention to that text. However I want to be able to toggle on and off the purple text. When I toggle off the purple I want the text to change to gray. I do need to be able to change it back to purple as well. Is there anyway to do this using script?
Thank you!

You can create a dedicated swatch and change its color components to fit your taste.
All references e.g. from within styles will follow.
// assuming the swatch is already created
var swatch = app.activeDocument.swatches.itemByName("purple");
swatch.properties = {model:1886548851, space:1129142603, colorValue:[0, 0, 0, 30]};

The following script changes the internal color setting of the specified character style, from a 'purple'-named swatch to a 'grey'-names swatch, and vice-versa.
var myDoc = app.activeDocument;
var charStyle = myDoc.characterStyles.item('My Character Style');
var purple = myDoc.colors.item('purple');
var grey = myDoc.colors.item('grey');
if (charStyle.fillColor == purple)
charStyle.fillColor = grey;
else charStyle.fillColor = purple;
If it's set to Purple then it changes to Grey, and if it's set to Grey it changes to Purple.
(change the swatch names and character style to suit your needs)

you can try this :
http://forum.jquery.com/topic/toggle-change-row-color
try to create a class for your "purple text" , and add/remove the class to change the colors.
there is a similar question at this link :
How do you toggle links to stay a certain color until clicked again

Related

Apply style using document.querySelector

I am trying to apply a grey background color to a div element inside a page.
Thats the one highlighted below:
I am able to apply the background color with the below code in the page onload:
var topBar = document.querySelector("td.top-bar div.top-bar");
alert(topBar);
topBar.style.backgroundColor="#808080";
However, there is a background property that is undoing what I applied:
How can I remove the highlighted "background" property?
I tried topBar.style.background = "".
But it doesnt work.
You are changing other css property. To make it work you have to overwrite background, not background-color. The easiest way is just to do this:
var topBar = document.querySelector("td.top-bar div.top-bar");
topBar.style.background="#808080";
It should works

How to change the color of the back ground to something the user inputs in HTML

I am trying to make my website so the user can input a color using a color selector, and then it changes the background color to that. This is what I have so far:
function color_change() {
alert("I will make it the color you have choosen.")
var back = document.getElementById("color")
var new_color = document.getElementById("change").value
// What goes here?
}
#color {
background-color: #FFFFFF;
}
<HTML>
<div id = "color">
Change the Color
<br>
<input id = "change" type = "Color">
<button onclick="color_change()">Make it this color!</button>
</div>
</HTML>
I tried that but all it does is come up with an error. Does anyone know how to make it work? I want it to have the user input a color by clicking the box. When they click the box, a Color Picker opens. Then, when they click the Make it this Color! button it will set the background of the div to the color they put in.
You need to set the background colour of the color div element to the newly selected colour. You have correctly managed to retrieve the colour value from the colour input element and stored it in the new_color variable.
You can apply CSS styling options to an element via the style object. This exposes the Stylesheet and the CSSStylesheet interfaces for you to manipulate. It can both get and set styles. In your case, you would like to set the background colour of an element. So you have to get the element with javascript which you have already done and then access the style object followed by the backgroundColor property which you can then set equal to the colour value of the colour input element:
document.querySelector("#color").style.backgroundColor = "{any css colour value}"
Below is adjusted snippet utilising this.
function color_change() {
alert("I will make it the color you have choosen.")
var back = document.getElementById("color")
var new_color = document.getElementById("change").value
// Apply background color to div#color element
back.style.backgroundColor = new_color;
}
#color {
background-color: #FFFFFF;
}
<div id="color">
Change the Color
<br>
<input id="change" type="color">
<button onclick="color_change()">Make it this color!</button>
</div>
You can read more about the style object and what it can do here.

Each (funcion) is not working when combined with if statement

I have a product with multiple color variation.
What I am trying to do is to only show the photos of the selected color on thumbnail. For example, for the same chair, there are blue, black, red and green color variations. When the red one is selected, the other photos of other colors will disappear in the thumbnail.
I made it working the desktop version using css, however, on the mobile version, I have not had any luck with the slider, I think I should try to match the selected color with the color attribute, and remove the data-slick-index attribute of the unselected ones so they will not appear on the thumbnail, but I am not sure why my codes are giving me errors.
$(funcion() {
$('li.product-single__thumbnails-item').each(function(){
var $mobilemodelselected = document.getElementById("FeaturedImage-product-template");
var $mobilemodelcolor = $mobilemodelselected.getAttribute("color-option");
var $mobilecolorcheck = $(this).attr('color-option');
if ($mobilemodelcolor != $mobilecolorcheck) {
$('li.product-single__thumbnails-item').removeAttribute('data-slick-index');
}
});
});

Styling span textnode without styling child nodes

So I'm creating a theme plugin for a forum where users often enter text with custom coloring. This is implemented as an inline style on a span.
The forum by default has a dark color scheme so most of the text is light. If I create a theme with a light color scheme this text would be hard to see.
I thought of using a CSS5 color filter that targets text with inline colors:
.Comment span[style^=color] {
filter: hue-rotate(180deg) invert(100%);
-webkit-filter: hue-rotate(180deg) invert(100%);
}
By inverting and rotating the color spectrum this turns a light blue color into a dark blue and light red into dark red, preserving the hue but making it darker. This actually works but it has the side effect of also inverting the colors of images and other elements embedded in the text.
I thought of doing another color inversion on child elements but this leaves images looking like junk since apparently hue-rotate is not very accurate at all.
A solution would be to have the CSS only target the text node of the span and not any child elements but that does not seem possible? Unless I'm missing something I don't see any selectors for text nodes.
Is there something I can do in jQuery to perform this color inversion? I'd rather not have to destroy all the coloring on the page as that would upset the users.
this Solution only goes lighter does not solve the problem:
I mis-read the question... I tried to delete this because it did not solve the problem for light backgrounds but it still appeared.
This can be accomplished by making use of rgba
Tested and works:
$(window).load(function() {
var col = $('.Comment').css('color').replace(')', ', 0.20)').replace('rgb', 'rgba');
$('.Comment').css('color', col);
});
var col converts rgb to rgba allowing for color percentage
Here's a solution that uses the javascript library tinycolor. A pure CSS solution would be much preferred but it doesn't seem possible.
$.getScript('https://rawgit.com/bgrins/TinyColor/master/tinycolor.js').done( function() {
$('.Comment span[style^=color]').each(function() {
var color = tinycolor($(this).css('color')).toHsl();
color.l = 1 - color.l;
$(this).css({'color': tinycolor(color).toRgbString()});
});
});

Change background color on elements with the same color

I'm trying to create a simple theme system here. I have few elements with red background color. I also have a button that will change the elements background color to green.
I'm trying to code but I couldn't figure out how can I select and change the bg color of all red elements to green!
For example, I have 4 divs. Two of these have a red header, and when I click the button these headers background color must be changed to green. It's Ok here, but the problem is that the divs are dynamically generated, so do I have do loop all the page to find the red bg color? Could someone enlight me? =)
Thanks a lot! =)
I think the best solution would be to use different stylesheet for each theme and keep the current theme value in SESSION or COOKIE or just pass it as URL argument. But that would be a server side solution.
On Client side, you can just use different classes for each theme and toggle them on the button push event using .toggleClass()
$('.green').toggleClass('red green');
The easiest way to do this would be to have a specific CSS class for each background color you're using. ex:
.color-red{ background-color: #FF0000; }
.color-green{ background-color: #00FF00; }
Once you do that you can select on the CSS class as well as set the CSS class:
$('#button-togreen').click(function(){
$('.color-red').removeClass('color-red').addClass('color-green');
}
It's kind of a round-about way of doing it, however there is no easy way to select on a CSS attribute (to do that you'd have to select all elements of the page and then check each one of them for the background color attribute which would be scarily inefficient...)
I don't know what you mean by headers, but I think this should do what you want:
$('.myDivs').filter(function(index){
return $(this).css('color') == 'red';
}).css('color', 'green');

Categories

Resources