Skip to content


Append To Select List If Option Isn’t Already There

 

I want to append to a select list only if the selected option is not already there. Currently, I have this:

$('#columnsAvailable').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");

Solution

var optionExists = ($('#columnsAvailable option[value=' + $(this).val() + ']').length > 0);

if(!optionExists)
{
    $('#columnsAvailable').append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
}

Posted in Javascript, jquery. Tagged with , , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

You must be logged in to post a comment.