[SalesForce] get commandlink value using JS

I have a link commandLink with value and onclick attributes, When I click on the link it should call a function in JS to get commandLink value. but it returns undefined in alert.
How can I get commandLink value ?

function callActionMethod(){
    var param1 = $j("#rowlink").val();
    alert(param1);
}

<apex:commandLink reRender="testlabel" 
                  id="rowlink" 
                  onclick="callActionMethod()" 
                  value="{!accounts.id}" />

Best Answer

You can pass the parameter directly to the function; there's no need to do this with jQuery (or any JavaScript), at all.

<script>
function callActionMethod(param) {
    alert(param);
}
</script>
<apex:commandLink value="{!accounts.id}" onclick="callActionMethod('{!accounts.id}')" />

Fully functional example:

<apex:page controller="myControllerA">
<script>
function callActionMethod(recordId) {
    readCell(recordId);
}
</script>
    <apex:form id="form">
        <apex:actionFunction name="readCell" action="{!readCellMethod}" reRender="form">
            <apex:param value="" name="recordId" assignTo="{!selectedId}"/>
        </apex:actionFunction>
        <apex:pageMessages/>
        <apex:repeat value="{!accounts}" var="account">
            <apex:commandLink value="{!account.name}" onclick="callActionMethod('{!account.id}'); return false;"/><br/>
        </apex:repeat>
    </apex:form>
</apex:page>
Related Topic