Steve
Steve

Reputation: 11

Javascript - if/and/else not working

First off ... I'm fairly new to javascript ...

Here's the code I'm currently using, however it doesn't seem to be working properly. Here's what I'm trying to accomplish ...

If something has the 'typeset' value of "Complete set" and it's 'main' value is anything but "A2 tandem", then it's title variable should be "Line Set w/ ALinks: ". If it has any other 'typeset' value or it's 'main' value is "A2 tandem", then it's title variable should be "Line Set: "

What's currently happening: It doesn't seem to be differentiating between 'main' value being "A2 tandem". It is still applying the "w/ ALinks: " title if the 'main' value is "A2 tandem".

Help?

    if (accCat == "Line Sets"){
        document.getElementById('o1').value = document.getElementById('main').value;
        document.getElementById('o2').value = document.getElementById('mainsize').value;
        document.getElementById('o3').value = document.getElementById('typeset').value;
        document.getElementById('o4').value = document.getElementById('typeline').value;

        var canopyValue = document.getElementById('main').value;
        var title;

        if ((document.getElementById('typeset').value == "Complete set") && (canopyValue != "A2 tandem")){
            title = "Line Set w/ ALinks: ";
        }else{
            title = "Line Set: ";
        }

Upvotes: 1

Views: 104

Answers (4)

Matteo Defanti
Matteo Defanti

Reputation: 212

You didn't close the opening bracket of the initial if. Anyway as you are new to Javascript consider that Javascript has 2 operators for EQUALS. Take a look at http://www.w3schools.com/js/js_comparisons.asp

Given that x = 5

== equal value

x == 8 gives false
x == 5 gives true

=== equal value and equal type

x === "5" false x === 5 true

Upvotes: 1

Henry A.
Henry A.

Reputation: 391

if (accCat == "Line Sets"){ //<-------- YOU DIDN'T CLOSE THIS
    document.getElementById('o1').value = document.getElementById('main').value;
    document.getElementById('o2').value = document.getElementById('mainsize').value;
    document.getElementById('o3').value = document.getElementById('typeset').value;
    document.getElementById('o4').value = document.getElementById('typeline').value;

    var canopyValue = document.getElementById('main').value;
    var title;

    if ((document.getElementById('typeset').value == "Complete set") && (canopyValue != "A2 tandem")){
        title = "Line Set w/ ALinks: ";
    }else{
        title = "Line Set: ";
    }

Add a } to the end of the code.

Also a quick tip:

Instead of typing document.getElementById("someId").value every single time, just make a function that will get the element.

var get = function(id){
    return document.getElementById(id);
}

Then you can just access it like get("o1").value = get("main").value

Upvotes: 2

DrZoo
DrZoo

Reputation: 1697

You're missing brackets on your first if. You opened it, but the closing bracket doesn't come until after your else.

Upvotes: 1

cbender
cbender

Reputation: 2251

You didn't close the original if. Add a } to the end.

Upvotes: 0

Related Questions