1

I want to dynamically calculate the percentage value and pass the percentage value to add class prefixing 'p' to it. For every value of it i have a css class existing in my css file.

Please help find a way to do so.

    ac.sumPer=((ac.sum/5)*100);
    ac.test='p'+ac.sumPer;
    console.log(ac.test);

    $('#change').addClass("c100 big dark ac.test");
3

2 Answers 2

3

You can try bellow. If you pass variable in double quote then its value will not be replaced.

ac.sumPer=((ac.sum/5)*100);
ac.test='p'+ac.sumPer;
console.log(ac.test);

$('#change').addClass("c100 big dark "+ ac.test);
Sign up to request clarification or add additional context in comments.

Comments

1

Here is how you can use addclass() with variable names. Below is also the image when you inspect using dev tools.

$(document).ready(function(){
var classname ="text";
	$('p').addClass("paragraph "+ classname);
})
.paragraph{
  font-weight:bold;
}
.text{
  color:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p>
Hello There
</p>

enter image description here

For you case the code should be:

ac.sumPer=((ac.sum/5)*100);
ac.test='p'+ac.sumPer;
console.log(ac.test);

$('#change').addClass("c100 big dark "+ac.test);

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.