Is there a way to get linear-gradient to work in jQuery? I’m having difficulty getting my code to work getting hidden input values and applying them to a div via inline CSS.
My code:
jQuery(document).ready(function ($) {
$(document).on( "click", '#update .acf-button-group', function() {
var color1 = $('#color1 input').val()
var color2 = $('#color2 input').val()
$("#output .acf-input").css({"background": "linear-gradient(top, (color1) 0%, (color2) 100%)"});
});
});
#output .acf-input {height:100px}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="color1">
<input type="hidden" name="acf[field_60d4d8ef09c78]" value="#0779bf">
</div>
<div id="color2">
<input type="hidden" name="acf[field_61f4e8ef1957b]" value="#48a9e4">
</div>
<div id ="output">
<div class="acf-input"></div>
</div>
<button id="update">Update</button>
2
Answers
You can use
linear-gradient(to top,${color1} 0%, ${color2} 100%)
where color1 & color2 will have value of inputs and then set this in youracf-input
via css.Demo Code :
You need to use
to top
instead oftop
.Also, you need to pass the variable with
`
and${}
.Also,
$(document).on( "click", '#update .acf-button-group'
you should pass a Element$("#update")
instead of'#update .acf-button-group'
.After all fix, it should work.