I am trying to use jquery to append the value selected from a dropdown menu to an element in the body.
To double check that the dropdown returns any values, I used the jquery to change the background color of the page, which works. However, I am not able to conditionally display the color name after() the <p></p> element:
Fiddle : https://jsfiddle.net/7765fvjf/
$('#dropdlist').on('mouseenter mouseleave', function() {
var $color = $('#dropdlist :selected').text();
$('body').css('background', $color);
var $colorName = $('.colorName');
var newColor = ("<p class='colorLabel'>" + $color + "</p>");
var $colorLabel= $('.colorName',$newColor);
if (!$colorLabel[0]) $colorName.after(newColor);
else $colorName.replaceWith(newColor)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id='dropdlist'>
<option value="green">green</option>
<option value="yellow">yellow</option>
<option value="blue">blue</option>
</select>
<p id='tt' class='colorName'>
The background color is:
</p>