9

I have this in my htm file

    <select id="SelItem">

    </select>

Within my javascript file I have a section for jquery I have the following

     var itemval= '<option value="OT">OT</option>';

     $("#SelItem").html(itemval);

Wouldn't the following populate my drop down with OT? It does not populate anything in the drop down

2
  • 2
    Looks fine to me. Are you running the code on DOM ready? Commented Apr 3, 2012 at 17:54
  • 1
    Did you include jQuery on your page? Commented Apr 3, 2012 at 17:56

3 Answers 3

14

You might want to consider using append:

//Creates the item
var itemval= '<option value="OT">OT</option>';

//Appends it within your select element
$("#SelItem").append(itemval);​

Example

Update

As js1568 pointed out - the problem is most likely stemming from the page not being loaded when the JS / jQuery code is being executed. You should be able to fix this with the following:

$(document).ready(function()
{
    //Creates the item
    var itemval= '<option value="OT">OT</option>';

    //Appends it within your select element
    $("#SelItem").append(itemval);​
});
Sign up to request clarification or add additional context in comments.

2 Comments

If the code in the OP is not working, what makes you think this code would?
@MДΓΓ - I was providing an alternative option and a working example of it. It appears the issue is most likely directly related to when the JS is being called however, much like js1568 pointed out.
1

You need to wait until the page has loaded before running this code. Try wrapping your code inside $(document).ready() function.

Introducing $(document).ready()

Comments

0

The problem is when the jquery is called, not what its doing.

$(document).ready(function() {
     var itemval= '<option value="OT">OT</option>';
     $("#SelItem").html(itemval);
});

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.