Title: If Condition for onclick not working
Tags: onclick;if
Question: I am looking for a way to insert a record and close the window with the VF-Page, when everything was entered correctly and user clicks button. The values are entered correctly, if a subject was entered. For that, I made a condition 

button type="button" onclick="if(theTask.Subject != '') {createTask();window.close();}"

Unfortunately, this condition is not recognized. Why? 

The controller is:

```public with sharing class LogACallExtension {
    private final SObject parent;
    public Task theTask {get; set;}
    public String lastError {get; set;}

    public LogACallExtension(ApexPages.StandardController controller) {
        parent = controller.getRecord();
        theTask = new Task();
        theTask.WhoId = parent.id;
        theTask.Status = 'Completed';
        theTask.ActivityDate = date.today();
        theTask.Type = 'Call';
        theTask.Priority = 'Normal';
        lastError = '';
    }

    public PageReference createTask() {
        createNewTask();
        theTask = new Task();
        theTask.WhoId = parent.id;
        return null;    }    

     private void createNewTask() {      
        try {
            insert theTask;            

        } catch(System.Exception ex){
           lastError = ex.getMessage();
        }
    } 
}  
```



Here is another answer: Edit

Trying to make as little changes as possible to your original code, this is something close to what you are looking for.

The idea behind is that when the controller succeeds(or fails), it will refresh the view. If it succeeds, it will render the javascript window.close() and close the window.

Anyway I have the feeling you forgot to put the ```lastError``` somewhere in the view to notify the users ;)

View :

```<apex:page standardcontroller="Lead" extensions="LogACallExtension" showHeader="false"&gt;
    <script type='text/javascript' src='/canvas/sdk/js/publisher.js'/&gt;
    <apex:form &gt;
        <apex:pageBlock title="Log a Call" mode="edit"&gt;
            <apex:actionFunction action="{!createTask}" name="createTask"  /&gt;
            <apex:pageBlockSection title="" columns="1"&gt;
                <apex:inputField value="{!theTask.Not_reached__c}"/&gt;
                <apex:inputField value="{!theTask.Subject}" id="test123" required="true" style="width: 570px; height: 20px" /&gt;
                <apex:inputField value="{!theTask.Description}" style="width: 600px; height: 200px"/&gt;
            </apex:pageBlockSection&gt;                    
            <button type="button" onclick="window.close();" style="height:20px;width:50px;" id="cancelTask"&gt;{!$Label.Cancel_Button}</button&gt;    
            <button type="button" onclick="createTask();" style="height:20px;width:50px;" id="addTaskButton"&gt;{!$Label.Save_Button}</button&gt;
            <script type="text/javascript"&gt;{!IF(success,'window.close();','')}</script&gt;
        </apex:pageBlock&gt;
    </apex:form&gt;
</apex:page&gt;
```

Controller

```public with sharing class LogACallExtension {
    private final SObject parent;
    public Task theTask {get; set;}
    public String lastError {get; set;}
    public Boolean success {get;set;}

    public LogACallExtension(ApexPages.StandardController controller) {
        parent = controller.getRecord();
        theTask = new Task();
        theTask.WhoId = parent.id;
        theTask.Status = 'Completed';
        theTask.ActivityDate = date.today();
        theTask.Type = 'Call';
        theTask.Priority = 'Normal';
        lastError = '';
    }

    public PageReference createTask() {
        createNewTask();
        theTask = new Task();
        theTask.WhoId = parent.id;
        success = true;
        return null;    }    

     private void createNewTask() {      
        try {
            insert theTask;            

        } catch(System.Exception ex){
           success = false;
           lastError = ex.getMessage();
        }
    } 
}
```

There is no actual need to check for the empty value of ```Subject``` because it is a required field, and therefor Salesforce will check it for you.

if you still want to check it yourself, you could put to the input a ```styleClass="js-subject"``` and in the onclick do something like ```if(document.getElementsByClassName('js-subject')[0].value !== ''){...```

Comment for this answer: This is my VF Page:
Comment for this answer: yes, I have an action function called createTask
Comment for this answer: Reply updated. I was actually expecting you to update your question, not posting the reply as an answer, so we keep the answers to a minimum and cleaner

{'source': 'StackExchangeDataset', 'subset': 'salesforce.stackexchange.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:10/30/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<onclick><if>', 'Title': 'If Condition for onclick not working', 'ViewCount': 299, 'id': 197273, 'user_AboutMe': None, 'user_AccountId': 12126365.0, 'user_DisplayName': 'Thomas', 'user_Reputation': 1.0, 'user_Views': 0.0, 'user_id': 50600.0}}