0

I would like to change an extra css property:value in the below script. I would like to add top:70%; when 'rotated text' is selected, how do I add it correctly?

I found some different options (e.g. $(".sw_poster_text2").css({"propertyname":"value","propertyname":"value"}); but that's not working in my script.

.trigger('change');
    $("select[data-field-id='5f01284fb2641']").change(function() {
        var transform = $(this).find('option:selected').data('wapf-label');
        if(transform == 'straight text')
            transform = 'rotate(-0deg)';
        if(transform == 'rotated text')
            transform = 'rotate(-40deg)';
        $(".sw_poster_text2").css("transform", transform);
    });  

2 Answers 2

1

css top should be px, em rather than %.

$("select[data-field-id='5f01284fb2641']").change(function() {
        var transform = $(this).find('option:selected').data('wapf-label');
        if(transform == 'straight text')
            transform = 'rotate(-0deg)';
        if(transform == 'rotated text') {
            transform = 'rotate(-40deg)';
            $(".sw_poster_text2").css({position: "absolute", top: "70px"});
        }
        $(".sw_poster_text2").css("transform", transform);
        console.log($(".sw_poster_text2").attr("style"));
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<select data-field-id="5f01284fb2641">
<option data-wapf-label="straight text">Straight text</option>
<option data-wapf-label="rotated text">Rotated text</option>
</select>
<div class="sw_poster_text2">Sample text</div>
</form>

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

Comments

0

If you make good use of Classes, you can more easily style the element.

$("select").change(function() {
  var transform = $(this).find('option:selected').data('wapf-label')
  var sample = $(this).siblings('.sw_poster_text2')
  sample.attr("class", "sw_poster_text2").addClass(transform);
});
.strait {
  top: inherit;
  transform: rotate(-0deg);
}

.rotated {
  position: absolute;
  top: 70%;
  transform: rotate(-40deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <select data-field-id="5f01284fb2641">
    <option data-wapf-label="straight text">Straight text</option>
    <option data-wapf-label="rotated text">Rotated text</option>
  </select>
  <div class="sw_poster_text2">Sample text</div>
</form>

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.