最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to get property 'options' of undefined or null reference - Stack Overflow

programmeradmin5浏览0评论

i am getting the above error in ie 10 please give me a suggestion.it is working fine in

function getSelectedIndex(element, value) {
    var selectedIndex = 0;
    for(i = 0; i < element.options.length; i++) {
        if(element.options[i].value == value) {
            selectedIndex = i;
            break;
        }
    }
    return selectedIndex;
}

element.options.length is giving Unable to get property 'options' of undefined or null reference.please suggest me a sample code. Edit : It was working for me when I was using IE11 with patibility mode, but when I removed it and ran it in normal mode, the above issue occurred.

i am getting the above error in ie 10 please give me a suggestion.it is working fine in

function getSelectedIndex(element, value) {
    var selectedIndex = 0;
    for(i = 0; i < element.options.length; i++) {
        if(element.options[i].value == value) {
            selectedIndex = i;
            break;
        }
    }
    return selectedIndex;
}

element.options.length is giving Unable to get property 'options' of undefined or null reference.please suggest me a sample code. Edit : It was working for me when I was using IE11 with patibility mode, but when I removed it and ran it in normal mode, the above issue occurred.

Share Improve this question edited Jan 18, 2016 at 8:00 Sukant M 358 bronze badges asked Sep 25, 2013 at 10:08 user2814612user2814612 411 gold badge1 silver badge5 bronze badges 6
  • 1 Assuming element is a <select> why don't you use element.selectedIndex? – Niccolò Campolungo Commented Sep 25, 2013 at 10:10
  • How to you call this function? – XCS Commented Sep 25, 2013 at 10:12
  • His problem is in loading the actual element. – mavrosxristoforos Commented Sep 25, 2013 at 10:12
  • when i am loading an element it is giving this error – user2814612 Commented Sep 25, 2013 at 10:15
  • Your problem is in one of three places: 1) the call to getSelectedIndex; 2) the HTML of the desired element; 3) the way the script is inserted in the HTML – Tibos Commented Sep 25, 2013 at 10:16
 |  Show 1 more ment

2 Answers 2

Reset to default 2

Use elements.options.indexOf(value) (assuming element is defined, which it doesn't seem to be). By the way, your current design will return zero if the first element matches or there is no match at all. If you really want to write your own version of indexOf, better to return -1 in the case of no match as it does.

actually the message gives you exactly what you need to know. you are trying to read a property of something that does not have a value, and here it is "element" or "element.options", check the place where they are set before the function call. for IE10 it's a strict one, it doesn't support the use of element.options.length

instead you should use:

document.getElementById("optionsID").value

I hope this helps

发布评论

评论列表(0)

  1. 暂无评论