Matthew Verstraete
Matthew Verstraete

Reputation: 6791

Using jQuery to increase the size attribute of HTML select box

I have an HTML select box with a size of 1 to start and would like to increase that each time a new option is inserting into the box. I am able to insert the options with no problem but I can't figure out the correct way to increase the size. I tried following this SO post but the code is not working and I am not really seeing what I am missing.

Here is the relevant code:

var currentSize = $("#SelectedProjects").attr("size");
currentSize++;
alert(currentSize);
$("SelectedProjects").attr("size", currentSize);

As a test I am alerting the currentSize and that alert returns the right number but when I try to assign it the box does not grow. What am I missing here?

Upvotes: 4

Views: 305

Answers (1)

Barry Michael Doyle
Barry Michael Doyle

Reputation: 10658

Your last line of code is missing the # refer to an element with the id of SelectedProjects. Instead, now it is looking for a SelectedProjects element.

Fix this by adding the missing #:

var currentSize = $("#SelectedProjects").attr("size");
currentSize++;
alert(currentSize);
$("#SelectedProjects").attr("size", currentSize);
<!-- ^^^^^ here -->

Hope this helps.

Upvotes: 3

Related Questions