Home > Enterprise >  jQuery get and set value in css attribute
jQuery get and set value in css attribute

Time:02-04

this is my simple code:

var qibla = $(".qd .wpb_wrapper").text();
        $('#qdir img').css('transform', 'rotate('  qibla  'deg)');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div >
    <div >
    <div >
    218.4
    </div>
    </div>
    <div  id="qdir"><div ><img width="300" height="300" src="https://s20.picofile.com/d/8447362450/a60ba95e-4386-462e-8f26-10d69847adbf/download_1.svg"  alt="" loading="lazy"></div></div></div>

I want to get text value from "qd" element, that is 218.4

ad then set it into "#qdir" as css rotation attribute. but not work.

CodePudding user response:

This is because the qibla variable has additional space and enter character at the beginning and the end. Just use trim() function to remove them.

More information: https://www.w3schools.com/jsref/jsref_trim_string.asp

var qibla = $(".qd .wpb_wrapper").text().trim();
$('#qdir img').css('transform', 'rotate('  qibla  'deg)');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div >
    <div >
    <div >
    218.4
    </div>
    </div>
    <div  id="qdir"><div ><img width="300" height="300" src="https://s20.picofile.com/d/8447362450/a60ba95e-4386-462e-8f26-10d69847adbf/download_1.svg"  alt="" loading="lazy"></div></div></div>

  •  Tags:  
  • Related