2

ok, I have a VF page. and some inputField as well as outputText, and I wanted to do some front end calculation on these data while a button is clicked. on the outputText, for example I have:

<apex:outputText id="processor_name" value="{!ProcessorName}" label="Processor" />

here is the button:

<button type="button" onclick="executeCalculation()">Calculate</button>

And here is the jquery

function executeCalculation(){  
                alert($j("[id$='num_locations']").val());  
}

the outputText field is displaying data from database just fine.

the alert message is just blank, not showing anything. if I change the script to

function executeCalculation(){  
        $j("[id$='num_locations']").hide();  
}

the field will hide just fine. I'm not sure why I can't get the data to display in the alert.

1
  • Please try to select the most appropriate tags and avoid using general tags. This will help get the right people to your question. Commented Jan 13, 2014 at 18:17

1 Answer 1

6

The jQuery .val() method is used to retrieve the value of an input element. You are attempting to use it to get the value from a non-input element which is why you are getting a null result.

Try using either of the two methods available for retrieving the contents of matched elements as text or html. (Usage depends on your specific need)

http://api.jquery.com/text/

function executeCalculation() {
    alert($j("[id$='num_locations']").text());
}

http://api.jquery.com/html/

function executeCalculation() {
    alert($j("[id$='num_locations']").html());
}
1
  • wow, didn't think about it at all. works perfect now. thanks so much Commented Jan 13, 2014 at 19:22

You must log in to answer this question.

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