You need to sign in to do that
Don't have an account?
Sabrent
Conditional function
I am trying to alert a reminder if file is not selected to upload. I know i am doing something wrong in the function displaymessage(). Can someone point me to a correct way of doing this. Thanks!
<apex:page id="thePage" standardController="Account" extensions="attachmentsample">
<script type="text/javascript">
function displaymessage(){
var fileInput = document.getElementsByNameId("{!$Component.thePage.theForm.pageBlock.pageBlockSection.fileUpload}");
if(fileInput=="" ||fileInput==null){
alert("You must attach documents !");
}
else {
alert("You have successfully attached documents");
}
}
</script>
<apex:form id="theForm">
<apex:sectionHeader title="Upload a Attachment into Salesforce"/>
<apex:pageblock id="pageBlock" >
<apex:pageblocksection id="pageBlockSection" columns="1">
<apex:inputfile id="fileUpload" value="{!myfile.body}" filename="{!myfile.Name}" />
<apex:commandbutton value="Click Here to Attach File" action="{!Savedoc}" onclick="displaymessage()" immediate="true"/>
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
Try changing
var fileInput = document.getElementsByNameId("{!$Component.thePage.theForm.pageBlock.pageBlockSection.fileUpload}");
to
Or keep and change the " to ' and add .value
Also change onclick="displaymessage() to onclick="return displaymessage();"
in the saveDoc() method make sure it is a pagereference and returns Null
Thanks @Starz26 I will try that.
Did as suggested, without success. What am I doing wrong?
Try changing to above. you still had the " " around !$Component.fileUpload instead of single ' '
Also, added a ; after displayMessage()
Java is very specific and while it may look ok, it is not.
You can test where the Java is failing by putting alert('Alert'); before a line in the script. If the messge pops up then the java runs at least until that point. Move the alert within the java script (like after the var fileinput) and if it poped up before then does not you know the var line is not working.
Others may have a better way to test the java but this is what I do to quickly debug where in the script is failing.
I tried as suggested without success.After moving the Alert('message') it seems that the following line is failing.
I also tried document.getElementsById instead of document.getElementsByNameId
Any suggestions?? Than you once again and I am very grateful for your time and help.
Someone with more experiece may need to chime in. I had the exact code I gave you working but it was using an inputText and not inputFile.
Sorry I could not be of more help.
Yeh you are right it works with inputText.
That's all right, I hope someone has a suggestion.
Unfortunately Salesforce Help Desk doesn't support javascript code.
Thanks. You have been very helpful.
Quick note, document.getElementsByNameId does not exist in JavaScript.
Try the following:
Yeh i tried that yet no alert. I must be doing something silly.
Also tried
document.getElementsById('fileUpload').value;
"Element" is not plural. IDs must be unique in html, meaning, you cannot have more than one element with "fileInput" as its ID.
It should be:
document.getElementById('fileUpload').value;
Not:
document.getElementsById('fileUpload').value;
Copy and paste the following:
That should work for you.
I very much appreciate the suggestions provided by everyone.
sadly this still doesn't work. It's very frustrating.
What browser are you using by chance?
The file input is an extremely pesky form field due to varying browser security restrictions.
Try it out in Chrome or Firefox if you haven't.
I tried it in firefox. No success.
Thanks for following it up. Much appreciated!!!