I am trying to create a function to update CSS based on user input and I have it working except for the CSS property parameter.
$(document).ready(function() {
$('input').blur(function() {
var inputName = $(this).attr("name");
updateCSS(inputName, 'button-style-one', 'background-color');
})
// Function to update CSS
function updateCSS(pInputNameAttribute, pElementClass, pCssProperty) {
var inputName = '[name=' + pInputNameAttribute + ']',
elementClass = '.' + pElementClass,
inputValue = $(inputName).val();
$(elementClass).css({
pCssProperty: inputValue
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="submit.php" method="post" id="buttonSettings">
<input type="text" name="button_background_color" placeholder="Background Color">
</form>
<a href="#" class="button-style-one">Button</a>
This works if I replace pCssProperty: inputValue with 'background-color': inputValue, but I need the CSS property to be a parameter of the function - how can I get this to work property with a variable? JSFiddle: https://jsfiddle.net/frafgdnq/10/
.css()method takes as an argument, you're allowed to have property names without quotes. Variables normally won't be interpolated on the left side of the colon. So, your code was trying to set a CSS property that was literally namedpCssProperty.