I have a visualforce page and an apex controller class. There is a string variable inside the apex class and i want to find the length of that string from visualforce page using javascript.
global with sharing class SampleClass{
public String sampleString{get; set;}
/***rest of the code**/
}
<apex:page showHeader="false"
controller="SampleClass">
<!--rest of the code-->
<script type="text/javascript">
/**code**/
console.log('LENGTH: ' + ({!sampleString}).length);
</script>
</apex:page>
When I run the above code, this: LENGTH: undefined
is what i am getting. And I have already checked whether there is value inside the string. sampleString
is not null and it has value inside it. So, what am i doing wrong? Is there any other way to do it?
system.debug(sampleString.length());
That aside, the Javascript property you're looking for islength
, notsize
length
. I just checked ifsize
was working and forget to change it back before copying the code. And also yes, I have to check it in javascript.