I have a list on my controller. I want its size in javascript function. How to get it?
-
If you are calling javascript function of Visualforce page then you can get the size in visualforce and pass it to your javascript function as an parameter.Pramod Kumar– Pramod Kumar2013-06-15 07:44:55 +00:00Commented Jun 15, 2013 at 7:44
-
please post your code or the related code snippet, so everyone can understand root cause of your issueAnshul– Anshul2015-11-08 18:40:18 +00:00Commented Nov 8, 2015 at 18:40
Add a comment
|
2 Answers
I would expect it'll be something like this in the HTML
The use of JSEncode is probably not necessary, but if you are mixing VF output and Javascript it's a good practice.
<script>
var myJavascriptVariable = {!JSENCODE(myVisualforceList.size)};
</script>
-
Getting incorrect syntax errorPankaj– Pankaj2013-06-15 07:12:12 +00:00Commented Jun 15, 2013 at 7:12
-
3You can bind
list.sizebut only for lists you've declared. You can't call size for a related list on an SObject, i.e.Account.Contacts.sizeis not gonna happen.2013-06-15 08:10:44 +00:00Commented Jun 15, 2013 at 8:10
You can use something like this in your javascript -
alert({!lstMySampleList}.length);