2

I have a bunch of divs that i'm using to dynamically change style with jquery.css(). The way I'm pulling the property is to return the div id of the container.

In this case, stylechange = "fontFamily" because that's the ID of the main container.

str = the value of the checked option.

I've console.log() and alert() to debug this, and i'm getting strings returned, and when i manually set the properties and values it works.

However, using variables as either the property or the value does not work, and I'm stumped.

Javascript:

var stylechange = "";
var str = "";
$("select").change(function(){
   stylechange = $("select option:selected").parents(".styleoptions").attr('id');
   str = $("select option:selected").text();
   $(".elementselected textarea").css({stylechange: str});
})

HTML: This is the div

<div class="ui-draggable ui-resizable addeddiv elementselected absolute move" style="position: relative; display: block;">
   <div class="ui-resizable-handle ui-resizable-e" style="z-index: 90; display: block;"></div>
   <div class="ui-resizable-handle ui-resizable-s" style="z-index: 90; display: block;"></div>
   <div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div>
   <div class="removethis ui-resizable-handle absolute">X</div>
   <textarea type="textbox" class="addedtext"></textarea>
</div>

This is the HTML that the css gets the ID from

<div id="fontFamily" class="styleoptions" style="display: block; text-transform: capitalize;">
   <div class="optiontitle" style="text-transform: capitalize;">family</div>
   <div class="options" style="text-transform: capitalize;">
      <select style="text-transform: capitalize;">
         <option style="text-transform: capitalize;">palatino</option>
         <option style="text-transform: capitalize;">arial</option>
         <option style="text-transform: capitalize;">verdana</option>
         <option style="text-transform: capitalize;">times new roman</option>
      </select>
   </div>
</div>
3
  • We'll need to see some HTML, but from the looks of it, you forgot to put the var in front of stylechange to actually declare it as a variable. And fontFamily isn't a CSS property, but font-family is Commented Jul 27, 2013 at 4:28
  • That sounds like a horrible idea! Using CSS properties as ID's ? Commented Jul 27, 2013 at 4:28
  • I added the HTML, and I did declare the variables beforehand. Commented Jul 27, 2013 at 4:41

1 Answer 1

2

Your code tries to set a css property called stylechange, which is not what you need, you wants to set the css property assigned to the variable stylechange

so you need to use

$(".elementselected textarea").css(stylechange, str);

Or

var style = {};
style[stylechange] = str;
$(".elementselected textarea").css(style );
Sign up to request clarification or add additional context in comments.

Comments

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.