0

Hello there ,

var s = "width:20px;height:25px;color:red;font-family:myfontfamily";

i have a string like this,now how can i change the values of width,height etc using javascript.there may be a \n after or before the properties (width,height etc)..

Ex : i want to change width to 50px on s. so it will be

var s = "width:50px;height:25px;color:red;font-family:myfontfamily";

My another replated Question is here Regular Expression to get text from css but it not working exactly what i need.

Please help me.

Thank you.

Update : You can see the Result on my fiddle here : http://jsfiddle.net/jitheshkt/s2DJR/14/

2
  • Hi. Instead of working with straight CSS strings, why don't you use js built-in styles' modifiers ? Like document.getElementById('thing').style.width = '50px'; Commented Nov 12, 2011 at 9:39
  • the string s is coming from a editor,not from a web page also i want to back data to that editor.also this is not a dom object,just string. Commented Nov 12, 2011 at 9:55

2 Answers 2

2

Regular expressions.

For example:

s = s.replace(/color:\s*([^;]+)/, "color:" + newvalue)
Sign up to request clarification or add additional context in comments.

2 Comments

Works well ,but i have a custom value there which is background:url({background}) code breaks here...is there any way to avoid this...
Hi,check this var s = "background-color:white;color:red;" s = s.replace(/color:\s*([^;]+)/, "color:" + 'green'); alert(s); it replacing the background-color value...is there any way to override this ?
1
var s = "width:20px;\nheight:25px;\ncolor:red;\nfont-family:myfontfamily";

alert( s.replace(/width:\d+/,'width:123').replace(/height:\d+/,'height:456') );

1 Comment

Thank you ,but works with int values only,i am failed to change the color value.

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.