I am trying to add a function into common.js that will change the background color of a button. Is this possible? thanks guys.
$('#api_search').style.backgroundColor('#e4e4e4');
I am trying to add a function into common.js that will change the background color of a button. Is this possible? thanks guys.
$('#api_search').style.backgroundColor('#e4e4e4');
You're using a jquery selector which means you'll have different functions/properties available to you than accessing DOM elements in vanilla javascript. Using this approach, you can set multiple css declarations in one function.
If you wanted to change other styling properties, you would just add them to the object passed as the argument to css
.
{backgroundColor:'#e4e4e4', color: '#fff', ...}
So you could use .css
as follows:
$('#api_search').click(function() {
$(this).css({backgroundColor:'#e4e4e4'})
});
try it here with a button
see jquery css()
documentation here
If you're trying to do this with Javascript, you could do something like the following:
document.getElementById("api_search").addEventListener("click", function(e) {
e.target.style.backgroundColor = "red";
});
Check this out for clarity:
$('#api_search').css("background","#e4e4e4");
or
$('#api_search').attr("style","background:#e4e4e4");