1

Whenever I am writing a javascript code to check if the input text is taking an empty value, it doesn't work. Please check what is the problem with my code?

Page Code Page Code

Controller Code

  • 1
    Hi Happy, thanks for posting your code - it makes it a lot easier to help you! Next time, please copy and paste the code rather than uploading a screenshot - it's easier to read and allows search engines to index its contents. – Rob Aug 12 '15 at 7:34
2

Open the page in your web browser and inspect (Right click > Inspect Element) the text input field. You'll find that its ID is not name, but something in the format of Parent1:Parent2:name.

This is because Salesforce does not simply output the ID given for the element in the Visualforce file, but also includes the names of element's parents, from outermost (here the apex:page) inwards. As a result, document.getElementsById('name') is failing to select the element.

To fix this, have a look at this post by the wonderful "Uncle" Bob Buzzard, aka Keir Bowden, aka Walter Sobchak. In short, you need to add id attributes to your Visualforce components and change the Javascript selector to use the format {!Component.PageName.FormName.PageblockName.InputTextName}.

Alternatively you could use a selector library such as the one in jQuery, but it looks like it would be overkill for this use case.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.