function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AK_SmithAK_Smith 

Run script only if checkbox field checked

Hello! how to run skript only if checkbox Project__c.check__c is checked
 
<script>
        var int=self.setInterval(function(){callActionFunction()},200);
        function callActionFunction(){
            ActionFunctionName();
        }

</script>

 
Best Answer chosen by AK_Smith
Aman MalikAman Malik
If this is read only, then it can't be fetch in javascript.
So, We need to access direct value in javascript through expression like below:
<script>
        var int=self.setInterval(function(){callActionFunction()},200);
        var x = {!Project__c.geo__c};
        function callActionFunction(){
        if(x){ 
            ActionFunctionName();
        }}

</script>
Above script must work for you.

Thanks
 

All Answers

AK_SmithAK_Smith
Did not work
<script>
        var int=self.setInterval(function(){callActionFunction()},200);
        var x = document.getElementById("geo").checked;
        function callActionFunction(){
        if(x.checked = true){ 
            ActionFunctionName();
        }}

</script>

 
Aman MalikAman Malik
Kindly provide your complete code snippet.
AK_SmithAK_Smith
<apex:page standardController="Project__c" id="page" docType="html-5.0">
<script>
geoFindMe()
function geoFindMe() {
  var output = document.getElementById("out");

  if (!navigator.geolocation){
    output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
    return;
  }

  function success(position) {
    var latitude  = position.coords.latitude;
    var longitude = position.coords.longitude;

    document.getElementById('page:form:lati').value = latitude.toString().replace(".",",")  ;
    document.getElementById('page:form:long').value = longitude.toString().replace(".",",")  ; 
    document.getElementById('page:form:SF1').checked = true;

   
  };

  function error() {
    output.innerHTML = "Unable to retrieve your location";
  };

  navigator.geolocation.getCurrentPosition(success, error);
}
</script>

   <script>
        var int=self.setInterval(function(){callActionFunction()},200);
        function callActionFunction(){
            ActionFunctionName();
        }

</script>

<apex:form id="form">

  <div style="display:none;"></div>
latitude :<apex:inputField id="lati"  value="{!Project__c.l__c}" /> <br></br>
longitude :<apex:inputField id="long" value="{!Project__c.ll__c}" />
<apex:inputField id="SF1" value="{!Project__c.SF1__c}" />
<apex:inputField id="geo" value="{!Project__c.geo__c}" />

<apex:actionFunction action="{!save}" name="ActionFunctionName"/>
                     
                     
                     
</apex:form>

</apex:page>

 
Aman MalikAman Malik
Hi,
Try below snippet:
<apex:page standardController="Project__c" id="page" docType="html-5.0">
<script>
geoFindMe()
function geoFindMe() {
  var output = document.getElementById("out");

  if (!navigator.geolocation){
    output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
    return;
  }

  function success(position) {
    var latitude  = position.coords.latitude;
    var longitude = position.coords.longitude;

    document.getElementById('page:form:lati').value = latitude.toString().replace(".",",")  ;
    document.getElementById('page:form:long').value = longitude.toString().replace(".",",")  ; 
    document.getElementById('page:form:SF1').checked = true;

   
  };

  function error() {
    output.innerHTML = "Unable to retrieve your location";
  };

  navigator.geolocation.getCurrentPosition(success, error);
}
</script>

   <script>
        var int=self.setInterval(function(){callActionFunction()},200);
        var x = document.getElementsByClassName('geo')[0].checked;
        function callActionFunction(){
        if(x){ 
            ActionFunctionName();
        }}

</script>

<apex:form id="form">

  <div style="display:none;"></div>
latitude :<apex:inputField id="lati"  value="{!Project__c.l__c}" /> <br></br>
longitude :<apex:inputField id="long" value="{!Project__c.ll__c}" />
<apex:inputField id="SF1" value="{!Project__c.SF1__c}" />
<apex:inputField id="geo" styleClass="geo" value="{!Project__c.geo__c}" />

<apex:actionFunction action="{!save}" name="ActionFunctionName"/>
                     
                     
                     
</apex:form>

</apex:page>


Please like the answer and mark it as best if this helps.

Thanks,
Aman
AK_SmithAK_Smith
Hi Aman. 
Nope. Its not working
maybe i need to use field ID from the record?
like var x = document.getElementsByClassName('Project__c.geo__c')[0].checked;
 
Aman MalikAman Malik
Suggestion: Try to debug each line of javascript code. May be some javascript statement breaking, due to which its not working.
 
AK_SmithAK_Smith
<script>
        var int=self.setInterval(function(){callActionFunction()},200);
        var x = true;
        function callActionFunction(){
        if(x){ 
            ActionFunctionName();
        }}

</script>

works if i use  var x = true/false. so the problem is in this part 
Aman MalikAman Malik
Code snippet shared by me should also work.
Kindly make sure you paste whole of the snippet in your page.
For your info, I updated one of apex:inputfield  in the code before like below:
<apex:inputField id="geo" styleClass="geo" value="{!Project__c.geo__c}" />

Thanks
AK_SmithAK_Smith
Project__c.geo__c = formula field, is it ok?
Aman MalikAman Malik
- Is the formula field return type is boolean?
- Is this field rendered as read only on your vf page?
- Is this field being shown on your vf page? If yes, what is the value in it?
AK_SmithAK_Smith
- yes 
- reed only
- yes shown and marked as checked reed only  
AK_SmithAK_Smith
User-added imageUser-added image
Aman MalikAman Malik
If this is read only, then it can't be fetch in javascript.
So, We need to access direct value in javascript through expression like below:
<script>
        var int=self.setInterval(function(){callActionFunction()},200);
        var x = {!Project__c.geo__c};
        function callActionFunction(){
        if(x){ 
            ActionFunctionName();
        }}

</script>
Above script must work for you.

Thanks
 
This was selected as the best answer
AK_SmithAK_Smith
Works!!!! Thank  you!
Aman MalikAman Malik
Hi there, 
Kindly mark my answer as best if that helped. 
Thanks
Aman