Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<div id="JS1" class="JS">
			<input type="text" id="color1" class="colorText" size='8'>
		</div>


document.getElementById("color1").addEventListener("click",
			function() {
				document.getElementById("JS1").style.backgroundColor = 
					document.getElementById("color1").value;
			});


What I have tried:

$('#color1').on('click',function(){
			$('#JS1').style.backgroundColor = $('#color1').value;
				});
Posted
Updated 6-Dec-21 22:33pm
v2

1 solution

In this particular instance:
JavaScript
$('#color1').on('click', function () {
	$('#JS1').css('background-color', $('#color1').val());
});

The css() method is used to apply a style property to the element, and the val() method can be used against form input elements to retrieve the value entered.

These are very basic jQuery functions, I recommend getting acquainted with the jQuery API Documentation[^] which should provide you with all of the information you need.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900