0

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>

4 Answers 4

2

It seems more useful to have the colorLabel span present from the start -- with an id -- and only update its contents, not the contents of the surrounding p which only complicates things.

Secondly, using the change event is more appropriate.

Finally, with trigger() you can launch the function upon page load as well:

$('#dropdlist').on('change', function()  {
  var $color = $('#dropdlist :selected').text();
  $('body').css('background', $color);
  $("#colorLabel").text($color);
}).trigger('change');
<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: <span id="colorLabel"></span>
</p>

Sign up to request clarification or add additional context in comments.

Comments

2

you can do it like so, add a "color" span and change it's content dynamically each time the user select a new color.

$('#dropdlist').on('mouseenter mouseleave', function()  {
  var $color = $('#dropdlist :selected').text();
  $('body').css('background', $color);
  // change the text of ".color" span
  $(".color").html($color);
})
<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: <span class="color"></span>
</p>

Comments

2

You had a syntax error: Uncaught ReferenceError: $newColor is not defined.

Updated JS code to:

$('#dropdlist').on('change', function()  {
  var $color = $('#dropdlist :selected').text();
  $('body').css('background', $color);
  var htmlTarget = $('.colorName span');
  var newColor = ("<p class='colorLabel'>" + $color + "</p>");
  var $colorLabel= $('.colorName',newColor);

  htmlTarget.html(newColor);
  //else $colorName.replaceWith(newColor);
});

This works. See jsfiddle: https://jsfiddle.net/7765fvjf/6/

2 Comments

Sorry my question was probably unclear, I was trying to append/replace the value as the if statement would suggest
Edited to replace existing string rather than append.
1

You were close. Just provide an id to the paragraph.

HTML :

<select id="dropdlist">
  <option value="volvo">green</option>
  <option value="saab">yellow</option>
  <option value="mercedes">blue</option>
</select>
<p id="para"></p>

JavaScript :

$('#dropdlist').on('change', function() {
  var $color = $('#dropdlist :selected').text();
  $('body').css('background', $color);
  var $colorName = $('.colorName');
  $('#para').html('The background color is ' + $color)
});

JSFiddle :

https://jsfiddle.net/nikdtu/weqo1y30/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.