2
    <select id="ChangeViewId" onChange="changeView(this)"></select>

   function setView() {
 ..................... 
   var innerCode = '';
    for(i=0; i<viewList.length; i++) {                      
      innerCode += "<option value="+viewList[i]+">"+viewList[i]+"</option>";
     }
        document.getElementById("ChangeViewId").innerHTML=innerCode;
    }

function changeView(view)
    {       
        alert(document.getElementById('ChangeViewId').value);
    }

The values are C, Cpp,Java program. when i select Java program only Java is displaying i want Complete value Java program. I have tried with both onSelect and onClick Please help me.

2 Answers 2

3

The script generates this HTML:

<option value=Java program>Java program</option>

That is, an option tag that has value "Java" and a property "program". You need to add quotes around the value:

innerCode += '<option value="'+viewList[i]+'">'+viewList[i]+"</option>";
Sign up to request clarification or add additional context in comments.

1 Comment

It is generally good practice to encase your Javascript strings with single quotes ' so that you can write html markup more easily.
-1

This should do it

function setView() 
{
   var innerCode = '';
   for(i=0; i< viewList.length; i++) 
   {                      
      innerCode += '<option value="'+viewList[i]+'">' + viewList[i] + '</option>';
   }

   document.getElementById("ChangeViewId").innerHTML=innerCode;
}

function changeView(view)
{       
    var objSel = document.getElementById('ChangeViewId');
    alert(objSel.options[objSel.selectedIndex].value);
}

1 Comment

I changed the way you got alerted as you didn't specify HTML version this should work 3.2 upwards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.