0

i have a problem with my code. I need to pass value from my pick list to a script. My app doesn't see value of that pick list. Have any solutions?

<apex:form >
<apex:outputLabel value="Type" />
<br/>
<apex:selectList id="Lista" value ="{!type1}" size="1">
    <apex:selectOptions value="{!Types}"/>
</apex:selectList>
<br/>
<div class="typesSection"></div>
<apex:outputLabel value="First/Last Name" />
<br/>
<apex:inputText value="{!searchText}" id="searchText"></apex:inputText>
<apex:commandButton value="change" onClick="change()"/>
</apex:form>

<script>
    function change(Lista){

        ss = document.getElementById(Lista).value;
        alert(ss);
    }
</script>

1 Answer 1

0

You can directly access apex variable in Javascript. This will work for you.

<script>
    {!type1}
</script>

If you want the select value then you can do something like

<apex:selectList styleclass="Lista" value ="{!type1}" size="1">
    <apex:selectOptions value="{!Types}"/>
</apex:selectList>

var e = document.getElementsByClassName("Lista")[0];
var strUser = e.options[e.selectedIndex].value;

We are using class Name because in VF ID is generate dynamically so either you need to use dynamic Id selector or simply use the class Name.

Note: you can also use apex:repeat to iterate list in Javascript.

<script>
      <apex:repeat value="{!accList}" var="acc"> 
        {!acc}
      </apex:repeat>
 </script>
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .