1
<select class="dName" name="dName">
    <option SELECTED value="#" DISABLED>========================</option>
    <option id="0" value="amensah">Abbey-Mensah, Michael</option>
    <option id="1" value="acharya">Acharya, Niraj</option>
    <option id="2" value="achonu">Achonu, Geoffrey C.</option>
    <option id="3" value="agustin">Agustin, Erie</option>
    <option id="4" value="agyemang">Agyemang, Kuragu</option>
</select>

I am using the ID in my Javascript:

if (i[iVar] == $(".dName").find('option:selected').attr('id')) {

To pull which option was selected.

Now let's say I want to delete options #2 for the dName select, instead of changing the #3 to #2 as ID and #4 to #3 as ID, can I use an array? This might be easy but having a 100+ options would make it really time consuming

Is there anyway to change the ID in the dName select so no matter what it is used as an array? Something like:

<option id="[]" value="amensah">Abbey-Mensah, Michael</option>

So if I delete an option in the middle of the list, it will keep the numbering structure in order?

RESOLVED:

if (i[iVar] == $(".dName").find('option:selected').attr('id')) {

Has been changed to:

if (i[iVar] == "the value of selection option") {

AND

document.getElementById('first').innerHTML = phyList[$(".dName").find('option:selected').attr('id')].firstName;

Has been changed to:

document.getElementById('first').innerHTML = phyList[$(".dName").find('option:selected').index()-1].firstName; //.index()-1 because the first option is "=" which is not being used

So I can completely get rid of the ID and not worry about deleting an option statement.

6
  • 3
    Than you actually don't need ID at all, but rather grab the index of your element on change. Commented May 1, 2013 at 14:11
  • the id attribute might not be the right attribute to use for this. Commented May 1, 2013 at 14:11
  • The way you are doing it, no. it won't maintain the numbering structure by itself. Why can't you use the value as the unique identifier, and if you wanted to access which index the option is, you can use jQuery Commented May 1, 2013 at 14:11
  • hey buddy you dont need the Id for each option to know the selected option.$('select[class]=dName option:selected') this way you can get the selected option Commented May 1, 2013 at 14:13
  • This is my page: interfaithmedical.com/checksite/search.htm (how would i use the INDEX to achieve it in my JavaScript code?) Commented May 1, 2013 at 14:13

1 Answer 1

4

You don't need IDs at all, whatever you delete (.remove() ?) in your select element take the index of the selected option:

just for example:

$('select').on('change', function(){
  alert( $(':selected').index() );
});

http://jsbin.com/akefaq/2/edit

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

6 Comments

Is it possible to look at the code: interfaithmedical.com/checksite/search.htm and let me know if i remove the .attr('id'); in my codes will it still work?
@SiKni8 why don't you test my suggestion?
Your suggestion would work perfect I know... I am just confused as to where to make the changes in my Javascript code to allow index to come in handy :|
Can i use the VALUE? instead so no matter where the name is located, it would pull up based on that?
@SiKni8 you build your script in a very strange way. I would suggest you to store your values into var instead or reusing them all over the code. It's really messy. All you need could be written in a couple of lines.
|

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.