3

I'm trying to add new options to the select tag, but my code is adding the variable name addedFont instead of the actual number (i.e. 20 pixels)

main.js

$("#add-font-size").click(function(){
    var addedFont = $("#fontSizeInput").val();
    $("#font-select").append("<option value = addedFont> addedFont pixels </option>");
});

index.html

<select class="custom-select mb-2 mr-sm-2 mb-sm-0" id="font-select">
    <option value="16" selected>16 pixels</option>
    <option value="17">17 pixels</option>
    <option value="18">18 pixels</option>
    <option value="19">19 pixels</option>
    <option value="20">20 pixels</option>
</select>

Selector's options after adding font size 21:

enter image description here

2
  • 1
    Assuming addedFont is a variable '<option value="'+addedFont+'"> addedFont pixels </option>' Commented Feb 28, 2017 at 6:38
  • a simple correction '<option value = "'+addedFont+'">'+ addedFont+' pixels </option>' Commented Feb 28, 2017 at 6:41

3 Answers 3

2

You need to use string concatenation.

$("#font-select").append('<option value="'+addedFont+'">' + addedFont + 'pixels </option>');

However, I would recommend you to create element using jQuery

$("#font-select").append($('<option>', {value : addedFont, text:addedFont + " pixels"});
Sign up to request clarification or add additional context in comments.

1 Comment

Neat strategy. Just would like to add that I think OP meant addedFont + ' pixels' for the text, rather than the literal 'addedFont pixels'.
0

You are enclosing the variable inside the double quotes so it is treated as a string not a variable, use like this

$("#font-select").append("<option value = "' + addedFont + '">" + addedFont + " pixels </option>");

Comments

0

String concatenation (+) is what you were looking for here.

 $("#font-select").append("<option value=\"" + addedFont + "\">" + addedFont + " pixels </option>");

However, in the spirit of other clever answers, I would like to introduce the surprisingly simple syntax new Option(text, value) which has excellent browser support:

$('#font-select').append(new Option(addedFont + ' pixels', addedFont))

Demo:

$('#add-font-size').click(function () {
  var addedFont = $('#fontSizeInput').val()
  $('#font-select').append(new Option(addedFont + ' pixels', addedFont))
})
#fontSizeInput { width: 5em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="custom-select mb-2 mr-sm-2 mb-sm-0" id="font-select">
    <option value="16" selected>16 pixels</option>
    <option value="17">17 pixels</option>
    <option value="18">18 pixels</option>
    <option value="19">19 pixels</option>
    <option value="20">20 pixels</option>
</select>


<!-- Demo Code -->
<hr>
<p><input id="fontSizeInput" type="number" value="21"> px</p>
<button id="add-font-size">Add Font Size</button>

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.