1

Well, I've run into a a problem. I'm in the middle of modifying an ATS system and need to add some functionality yet I've run into a strange thing in JavaScript. Basically I need to change a set of form values based on a selection from a drop down menu and the code works... when there is only one thing to change. The moment I add other sections that need to change it stops working. I've ran all over the internet and many Stack questions but nothing seems to help. The code that works is:

<form id="test" name="test">
        <select id="statusID" name="statusID" onchange="javascript: return updateForm();">
                <option value="" selected></option>
                <option value="test1">One</option>
                <option value="2">Two</option>
        </select>
        <br />
        <input type="hidden" value="Test" id="RS" name="RS" />
    </form>

<script type='text/javascript'>
function updateForm()
{   
    if (document.getElementById('statusID').selectedIndex  == "2") 
                document.getElementById('RS').value = '200' 

                else if(document.getElementById('statusID').selectedIndex == "1")
                document.getElementById('RS').value = 'RSP'
}
</script>

This works when I test it on the server yet when I add the other fields that need to be populated it stops working altogether. The JavaScript I'm using is below. I've tried a few variations based on code I've seen but to no avail.

<script type='text/javascript'>
function updateForm()
{

    if (document.getElementById('statusID').selectedIndex  == "2") 
                document.getElementById('RS').value = '200'
                document.getElementById('RSDATE').value = '200'
                document.getElementById('RSP').value = '200'

                else if(document.getElementById('statusID').selectedIndex == "1")
                document.getElementById('RS').value = 'RSP'
                document.getElementById('RSDATE').value = 'RSP'
                document.getElementById('RSP').value = 'RSP'          
            }

</script>

I should note that I am in no way proficient with JavaScript at all. Nor am I proficient in PHP... yet I'm modifying a PHP based ATS system. This code is the second to last block between me and the goal (finishing the ATS system) so help would be greatly appreciated.

3 Answers 3

2

From the question's context, you should add braces following if and else if: (also noted by Combat, semicolons should be added)

        if (document.getElementById('statusID').selectedIndex  == "2")  {
            document.getElementById('RS').value = '200';
            document.getElementById('RSDATE').value = '200';
            document.getElementById('RSP').value = '200';
        }
        else if(document.getElementById('statusID').selectedIndex == "1") {
            document.getElementById('RS').value = 'RSP';
            document.getElementById('RSDATE').value = 'RSP';
            document.getElementById('RSP').value = 'RSP';      
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, it worked. Then why does it work without brackets? And why is it every time I run into a problem it ends up being I missed a bracket or a semi-colon? Gah! It makes my effort seem so fruitless. Thank you very much for the super fast response. Already tested it and it works.
The brackets "group" together all the statements that should be executed when if is true. Consider making a new function, where brackets are mandatory. When the end bracket is placed, the computer knows to stop. Leaving out brackets for if statements is a shortcut and only the next statement executes if true. About semicolons, JavaScript will silently insert them if omitted. (I don't know all the rules but you might run into mysterious bugs if you never put them. So it's best to always add them. Here's a page I found amusing on the subject)
Interesting read, playing safe is always better when it comes to code. Once again, thank you very much. I've been hitting the code too hard this week so I'm a little off my game. You have no idea how much pressure has been relieved by a few brackets.
2

You need semi colons at the end of each line as well as curly braces for each conditional.

function updateForm() {
    if (document.getElementById('statusID').selectedIndex == "2") {
        document.getElementById('RS').value = '200';
        document.getElementById('RSDATE').value = '200';
        document.getElementById('RSP').value = '200';
    }

    else if (document.getElementById('statusID').selectedIndex == "1") {
        document.getElementById('RS').value = 'RSP';
        document.getElementById('RSDATE').value = 'RSP';
        document.getElementById('RSP').value = 'RSP';
    }
}​

3 Comments

Funny thing about the curlies, in some of the javascript on the ATS it breaks the site (white screen) even though it shouldn't and others, if I don't use curlies, break the site... very odd.
I've never seen properly used curly braces break anything.
Neither have I yet the ATS system is very weird in general so I assume it is just the ATS being odd.
0

the conditionals need to be followed by brackets when you want to perform a block of actions. For example:

if (document.getElementById('statusID').selectedIndex  == "2") {
            document.getElementById('RS').value = '200'
            document.getElementById('RSDATE').value = '200'
            document.getElementById('RSP').value = '200'
}

1 Comment

Conditionals require curly braces when performing more than one operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.