2

Currently I am trying to write a method that appends the value of a dropdown menu to an html link. However I an having trouble retrieving the text of the selection from the menu. I narrowed the problem down to the selectedIndex method in Javascript. It returns undefined. I've included my method below.

function getSize(productID){
    var sizeBox = document.getElementsByName(productID);
    alert(document.getElementsByName(productID).selectedIndex);
    var sizeSelected = sizeBox.options[sizeBox.selectedIndex].text;
    alert(sizeSelected);
    var link = document.getElementById(productID).getAttribute("href");
    link = link + "&size=" + sizeSelected;
    document.getElementById(productID).setAttribute("href",link);
    return true;
}
3
  • why not put sample code somewhere like jsfiddle, and yet it is hard to read/understand formatting, do not hesitate about spacing and anything which adds to code more readability) Commented Nov 27, 2012 at 22:06
  • So what do the alerts show? Commented Nov 27, 2012 at 22:47
  • Thanks everyone! Problem solved! I was having problems with the parameter. Commented Nov 27, 2012 at 23:08

1 Answer 1

4

getElementsByName returns an HTMLCollection. You should say: getElementsByName[0]

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

1 Comment

Thanks! I tried the following adjustments: var sizeBox = document.getElementsByName(productID)[0]; and var sizeSelected = sizeBox.options[sizeBox.selectedIndex].text;. Both still return undefined.

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.