0

I have a selectlist in my apex:repeat. On click of command button i want to get the selected values in all the rows in my js function. My code:

    <apex:repeat value="{!myWrapper}" var="wrap">
    <tr>

     <td>
     <apex:selectList 
    id="selected" value="{!wrap.selected}" size="1"   style="width: 100%;" >

    <apex:selectOptions value="{!wrap.SelectList}">
    </apex:selectOptions>
      </apex:selectList>
    </td>
    </tr>
    </apex:repeat>
     <apex:commandButton onclick="if(!fncInitiate()){return false};return 
false;" value="Perfios Initiate - Applicant" style="width:19%;color: #0072bc;" reRender=""/>

js:

function fncInitiate(){
 //get the selected value from all the rows here
}                                                      
2
  • Could you state your problem?
    – Raul
    Apr 10, 2017 at 10:05
  • @Rahul, can you please let us know the solution for the same. What resolved your issue.
    – Nehal
    Jun 27, 2019 at 6:40

3 Answers 3

1

Define a Id on tag in your visualforce page

<apex:repeat value="{!myWrapper}" var="wrap">
<tr>

 <td id="SelectedComponent">
 <apex:selectList 
id="selected" value="{!wrap.selected}" size="1"   style="width: 100%;" >

<apex:selectOptions value="{!wrap.SelectList}">
</apex:selectOptions>
  </apex:selectList>
</td>
</tr>
</apex:repeat>
 <apex:commandButton onclick="if(!fncInitiate()){return false};return 
false;" value="Perfios Initiate - Applicant" style="width:19%;color: #0072bc;" reRender=""/>

Then get the selected value in the javaScript method through the help of this Id.

JS:-

function fncInitiate(){
//get the selected value from all the rows here
var selectedValue = $("#SelectedComponent").find(":selected").text();
console.log(selectedValue);
}   
0

You have to find all the dropdown list inside the table element and loop through each to get the selected values. Create a JavaScript Array object and push all the selected values into it.

<apex:page controller="MyController5" >
    <script src="https://code.jquery.com/jquery-1.8.2.js"></script>
    <script>
        var j$ = jQuery.noConflict();
        var selectValues = new Array();
        function fncInitiate(){
            selectValues = [];
            //get the selected value from all the rows here
            j$('#mytable').find('select').each(function() {
                selectValues.push(j$(this).val());
            });
            console.log(selectValues);
            return true;            
        }
    </script>    
    <apex:form>
        <table  id="mytable">
            <apex:repeat value="{!items}" var="wrap">
                <tr>
                    <td>
                        <apex:selectList value="{!wrap.selectedCarId}" size="1" id="carSelect" >
                            <apex:selectOptions value="{!wrap.cars}"/>
                        </apex:selectList>
                    </td>
                </tr>
            </apex:repeat>
        </table>
        <apex:commandButton onclick="if(!fncInitiate()){return false};return false;"/>
    </apex:form>
</apex:page>
3
  • can you explain how can it be achieved using Javascript, instead of jquery
    – Nehal
    Jun 27, 2019 at 6:39
  • @Nehal - Refer to Amit's answer to do the same in pure JavaScript.
    – Saroj Bera
    Jun 28, 2019 at 4:54
  • not working for me. As I have multiple picklist on my page.
    – Nehal
    Jul 1, 2019 at 12:08
0

Refer following code

    <apex:page showHeader="false" controller="Sample" >
<script>
function fncInitiate(){
 //get the selected value from all the rows here
// var picklistselected = document.getElementsByTagName("select");
   var picklistselected = document.getElementById('{!$Component.formId.pageBlockId}').getElementsByTagName("select");

 for(var i=0; i<picklistselected .length; i++)
{
    var e = picklistselected[i];
    var picklist = e.options[e.selectedIndex].value;
   console.log(i +" "+ picklist );
}

}
</script>
<apex:form Id = "formId">
<apex:pageBlock Id = "pageBlockId">
<apex:repeat value="{!wrapper}"  var  = "w" Id="repeatid">
     <apex:selectList id="selected" value="{!w.picklist}" size="1" >
    <apex:selectOption itemlabel = "1" itemvalue="1"/>
    <apex:selectOption itemlabel = "2" itemvalue = "2"/>
      </apex:selectlist><br/>
      </apex:repeat >
</apex:pageBlock>
     <apex:commandButton onclick="if(!fncInitiate()){return false};return 
false;" value="Perfios Initiate - Applicant" style="width:19%;color: #0072bc;" reRender=""/>
</apex:form>    
</apex:page>
2
  • You are looping through all dropdown elements present in the page document.getElementsByTagName("select");. Instead, you should loop through the dropdowns available inside the table.
    – Saroj Bera
    Apr 10, 2017 at 13:24
  • @Amit, not working for me. As I have multiple picklist on my page.
    – Nehal
    Jul 1, 2019 at 12:08

You must log in to answer this question.

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