Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new (not change) stylesheets using jQuery

We've got a little tool that I built where you can edit a jQuery template in one field and JSON data in another and then hit a button to see the results immediately within the browser.

I really need to expand this though so the designer can edit a full CSS stylesheet within another field and when we render the template, it will have the CSS applied to it. The idea being that once we've got good results we can take the contents of these three fields, put them in files and use them in our project.

I found the jQuery.cssRule plugin but it looks like it's basically abandoned (all the links go nowhere and there's been no development in three years). Is there something better or is it the only game in town?

Note: We're looking for something where someone types traditional CSS stylesheet data in here and that is used immediately for rendering within the page and that can be edited and changed at will with the old rules going away and new ones used in their stead. I'm not looking for something where the designer has to learn jQuery syntax and enter in individual .css("attribute", "value") type calls to jQuery.

like image 522
John Munsch Avatar asked Aug 19 '11 14:08

John Munsch


People also ask

How to set style dynamically in JavaScript?

color = "red"; you can apply the style change dynamically. Below is a function that turns an element's colour to red when you pass it the element's id . You could also use setAttribute(key, value) to set a style on an element. For example, you could set the colour of an element to red by calling element.

Can you dynamically change CSS?

If you want to change the CSS styles dynamically you'll have to attach this portion of code to some event. For example, if you want to change the styles of your element when clicking on a button, then you have to first listen to the click event and attach a function containing the previous code.

Can we add style in the JQuery?

We can apply styles on elements by using either the JQuery CSS Classes or the JQuery . css() method.


2 Answers

Sure, just append a style tag to the head:

$("head").append("<style>p { color: blue; }</style>");

See it in action here.

You can replace the text in a dynamically added style tag using something like this:

$("head").append("<style id='dynamicStylesheet'></style>");
$("#dynamicStylesheet").text(newStyleTextGoesHere);

See this in action here.

like image 119
Peter Olson Avatar answered Sep 24 '22 10:09

Peter Olson


The cleanest way to achieve this is by sandboxing your user-generated content into an <iframe>. This way, changes to the CSS won't affect the editor. (For example, input { display:none; } can't break your page.)

Just render out your HTML (including the CSS in the document's <head>, and write it into the <iframe>.

Example:

<iframe id="preview" src="about:blank">

var i = $('#preview')[0];
var doc = i.contentWindow || i.contentDocument;
if (doc.document) doc = doc.document;
doc.open('text/html',true);
doc.write('<!DOCTYPE html><html>...</html>');
doc.close();
like image 35
josh3736 Avatar answered Sep 21 '22 10:09

josh3736