0

I'm still pretty new to JavaScript programming and I'm having an issue.

JSFiddle: http://jsfiddle.net/Y4pPj/

HTML:

<form id="practiceForm" style="display:inline">
    <select id="practiceList" size="1">
        <option value="sampleA" selected="selected">Sample A</option>
        <option value="sampleB">Sample B</option>
        <option value="sampleC">Sample C</option>
    </select>
</form>
<button onclick="changeSelection();">Select</button><br />

<span id="currentSelected">Nothing selected</span><br /><br />
Name: <span id="name"></span><br />
Size: <span id="size"></span><br />
Shape: <span id="shape"></span><br />
Value: <span id="value"></span>

JavaScript:

/* var stats = {
    sampleA: { name: "Alpha", size: 3, shape: square, value: 1 },
    sampleB: { name: "Delta", size: 1, shape: circle, value: 10 },
    sampleC: { name: "Gamma", size: 25, shape, triangle, value: 200 }
}; */

function changeSelection() {
    document.getElementById("currentSelected").innerHTML = practiceForm.practiceList.options[practiceForm.practiceList.selectedIndex].value;
    document.getElementById("name").innerHTML = currentSelected.name;
    document.getElementById("size").innerHTML = currentSelected.size;
    document.getElementById("shape").innerHTML = currentSelected.shape;
    document.getElementById("value").innerHTML = currentSelected.value;
}

What I'm trying to do is, have a combobox list of items with a button. When you click the button, I need to change parts of the HTML to values that I've set in variables at the start.

The code, as it stands, works in JSFiddle. But, as soon as I un-comment the variable declaration, 'stats', it messes up, which really confuses me. Also, please excuse all the undisciplined stuff, like inline styles :) This was just for debugging test.

One more thing while I'm here- is there a way to shorthand the first line in the changeSelection() function? I've seen many examples, but they are all different.

3 Answers 3

1

It wont work like this. you need to access the stats through the selected value.

var stats = {
    'sampleA': { name: "Alpha", size: 3, shape: 'square', value: 1 },
    'sampleB': { name: "Delta", size: 1, shape: 'circle', value: 10 },
    'sampleC': { name: "Gamma", size: 25, shape: 'triangle', value: 200 }
};

function changeSelection() {
    document.getElementById("currentSelected").innerHTML = practiceForm.practiceList.options[practiceForm.practiceList.selectedIndex].value;    
    document.getElementById("name").innerHTML = stats[practiceList.value].name;
    document.getElementById("size").innerHTML = stats[practiceList.value].size;
    document.getElementById("shape").innerHTML = stats[practiceList.value].shape;
    document.getElementById("value").innerHTML = stats[practiceList.value].value;
}

look at this fiddle. http://jsfiddle.net/9QXc4/

Sign up to request clarification or add additional context in comments.

2 Comments

That's perfect, thank you. Still trying to absorb the why's, but that works when I apply it to my main project. I've also noticed that you put quotes on the objects themselves. Is there a difference? The code seemed to work without it, but I want to make sure.
If you don't put quotes, JS is expecting a object, not a value. It should not work without actually. But maybe is miss something. Anyway, with quotes you are on the save side.
0

It's throwing a syntax error in the console due to your object.

var stats = {
    sampleA: { name: "Alpha", size: 3, shape: "square", value: 1 },
    sampleB: { name: "Delta", size: 1, shape: "circle", value: 10 },
    sampleC: { name: "Gamma", size: 25, shape: "triangle", value: 200 }
}; 

The above is the correct syntax. Things needed fixing:

-Quote property values! square, circle, and triangle were unquoted. -The "shape" property had a comma rather than a colon to declare the value

Use the console when programming, you'll see this error right away:

Uncaught SyntaxError: Unexpected token , 

Which was caused by:

sampleC: { name: "Gamma", size: 25, shape, "triangle", value: 200 }
                                        ^^

1 Comment

Those damn noob mistakes... I do actually use the console, and it did catch some errors in the main project, but got lazy when I re-created for testing in JSFiddle :\
0

Just replace sampleC: { name: "Gamma", size: 25, shape, triangle, value: 200 } with this sampleC: { name: "Gamma", size: 25, shape: triangle, value: 200 }

You put a comma instead of a colon

1 Comment

also add quote around shape values like so: shape: "triangle"

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.