0

Please take a look at the following html. EDIT: UPDATED THE HTML PART

<div id="html_editor">

      <head>
       <style type="text/css" >
          .blog
          {
           border:2px solid grey;
           width:auto;

          }
          <style>{customcss}</style>
        </style>
        </head>
        </html>
</div>

Please take a look at the Css Class 'blog',i want to add some other values to that class through js/jQuery. Actually it is a HTML editor ,on the body tag user selecting a the 'blog' element,so that time i want to give the user to set CSS for the blog,user changing the CSS on a text area,after that i want to append/rewrite the data to that 'blog' class.

Ex : user setting the class like the following

width:250px;
background:red;
key:value..etc..

so after that i want to change that 'blog' css class to

.blog
          {
          width:250px;
    background:red;
    key:value..etc..

          }

How can i achieve this ? is there any way by using jQuery ??

UPDATE : Please check this image.

http://imgur.com/10JuB

Thank you.

4 Answers 4

1

For an HTML like this:

<style id="mycss" type="text/css" >
      .blog
      {
       border:2px solid grey;
       color:black;
       }
 </style>

<div class="blog">This is a blog</div>

Try this js:

var style = document.getElementById("mycss");
newrule = document.createTextNode('.blog { color:red;}');
style.appendChild(newrule);

This isn't very efficient as it overrides the previous rule, but you can get the general method.

JSFiddle here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you,this is what i need,but i changed my mentality [Your last line],the main problem is users wont put id for style,also its dynamic in structure.So let me do some database operations,saving the css value on database then retrieving on demand...
0

I went ahead and did the following test because I haven't done JavaScript in a while and I wanted to give it a go. The first method uses String.split to parse the textarea input and the second some basic regex. The regex will fail if there's more than one statement per line. They both put a syntax burden on the user greater than native CSS, so:

I think you should do what nikan suggested.

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
            google.load("jquery", '1.6.4');
            console.debug('loading');
            google.setOnLoadCallback(function() {
                var input = $('#userinput').val();

                var statements = input.split(';');
                for (var statement in statements){
                    var style = statements[statement].split(':');
                    var name = $.trim(style[0]);
                    var value = $.trim(style[1]);

                    $('#target').css(name, value);
                }

                var very_basic_css_matching = /^ *([^:]+): *([^;]+);/gm;
                while (matches = very_basic_css_matching.exec(input)){
                    $('#target').css(matches[1], matches[2]);
                }
            });
        </script>
    </head>
    <body>
        <textarea id="userinput">
width:250px;
height:10px;
background:red;
        </textarea>

        <div id="target">
        </div>
    </body>
</html>

Comments

0

With jquery is easy to access the current style and modify it:

http://jsfiddle.net/jedSP/7/

Try writing "color:white" or "background:green" in the text area, works in all browsers. When the user is done just use $("#style").html() to get the current CSS.

EDITED: Updated link... like this?

Comments

0

if you want this to happen in real time, its as easy as taking the value of the textarea and parsing it in javascript, and then applying the values with jquery like so

$('.blog').css({'property1':'value1','property2':'value2'});

now if you want to save these changes permanently, you will need to send the new css values to your server and store them in a database or something.

EDIT: to save it on the database...

You can get the value from the textfield like so. var cssVal = $('#textfieldid').val();

And then use the jQuery ajax function to send the new value to your server.

http://api.jquery.com/jQuery.ajax/

I'm not going to go into all of the details of this, you can find database tutorials everywhere online, but then you want to take the value of that textfield that you sent to your server using jQuery, and save it in your database table that stores the css rule properties.

Then when you regenerate the page, you will just retrieve the new css value from your database.

6 Comments

No,this is not what i need,i want to append the date to the two scopes on tht blog class.
I don't see how that's not doing that, at run time any elements that have the blog class will get the new styles applied to them. However, if you intend on adding new elements with the blog class then nikens way would be best.
i edited the Question,this is not happening on run time,Niken suggested a method but its not suitable here.
You should clarify what our solutions are missing, because as far as I can tell we have all suggested the same basic thing. So what is it we are missing? Just based on your question it still seems that the answers provided will work. Are you adding elements with the blog class after the user has saved css changes? Do you need the css to be saved so that if they refresh the page their changes will still be there?
Sorry,the actual problem is i dont want to apply CSS here,i just want it as TEXT for future,i mean that i am creating CSS CLASSES here and saving it on the database,ur answer is good,if i need to apply the style on here,$('.blog').css({'property1':'value1','property2':'value2'}); will only add it dynamically,i need its static code,so that i can save.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.