javascript - Code to detect option value does not work as expected - Stack Overflow

I was attempting to do some string parisons in javascript. I have seen several tutorials and examples b

I was attempting to do some string parisons in javascript. I have seen several tutorials and examples but they do not seem to work. I am missing something fundamental?

Attempt 1

function addAspect(aspect) {
    var print = document.createElement('p');
    var ptext;

    if (aspect == "Option1") ptext = document.createTextNode("This is option1");
}

Doesnt work.

Then I found this example to which all readers said it worked fine

function addAspect() {
    var print = document.createElement('p');
    var ptext;

    var aspect = String(document.getElementById("aspectResult").value);

    if (aspect == "Option1") ptext = document.createTextNode("This is option1");
}

Doesnt work.

I also tried .toString() as well as the '===' exact match parison.

Full code

<html>
    <head>
    <script type="text/javascript">
        function addAspect()
        {
            var print = document.createElement('p');
            var ptext;
            var aspect = document.getElementById("aspectResult").value;
            if (aspect == "Option1"){
                ptext = document.createTextNode("This is option1");
            }
            print.appendChild(ptext);
            document.getElementById("mainBlock").appendChild(print);
        }
        </script>
    </head>
    <body>
        <form>
            <select id="aspectResult">
                <option value="Option1">Option1</option>
            </select>
            <input type="button" value="Check" onclick="addAspect()"/>
        </form>
        <span id="mainBlock">&nbsp</span>
    </body>
</html>

Any suggestions?

I was attempting to do some string parisons in javascript. I have seen several tutorials and examples but they do not seem to work. I am missing something fundamental?

Attempt 1

function addAspect(aspect) {
    var print = document.createElement('p');
    var ptext;

    if (aspect == "Option1") ptext = document.createTextNode("This is option1");
}

Doesnt work.

Then I found this example to which all readers said it worked fine

function addAspect() {
    var print = document.createElement('p');
    var ptext;

    var aspect = String(document.getElementById("aspectResult").value);

    if (aspect == "Option1") ptext = document.createTextNode("This is option1");
}

Doesnt work.

I also tried .toString() as well as the '===' exact match parison.

Full code

<html>
    <head>
    <script type="text/javascript">
        function addAspect()
        {
            var print = document.createElement('p');
            var ptext;
            var aspect = document.getElementById("aspectResult").value;
            if (aspect == "Option1"){
                ptext = document.createTextNode("This is option1");
            }
            print.appendChild(ptext);
            document.getElementById("mainBlock").appendChild(print);
        }
        </script>
    </head>
    <body>
        <form>
            <select id="aspectResult">
                <option value="Option1">Option1</option>
            </select>
            <input type="button" value="Check" onclick="addAspect()"/>
        </form>
        <span id="mainBlock">&nbsp</span>
    </body>
</html>

Any suggestions?

Share Improve this question edited May 1, 2016 at 6:05 Brock Adams 93.7k23 gold badges241 silver badges305 bronze badges asked May 20, 2011 at 9:47 uremaurema 7314 gold badges11 silver badges22 bronze badges 16
  • Can you show the html for aspectResult? – Richard Dalton Commented May 20, 2011 at 9:50
  • do an alert on aspect on the line before the if statement and see what is in the popup - alert(aspect); – Andrew Commented May 20, 2011 at 9:52
  • <select id="aspectResult"> <option value="Option1">Option1</option> </select> - one option so that hings can not get confusing. It is in a <form> element. Thanks. U. – urema Commented May 20, 2011 at 9:53
  • the alert works, and it says 'Option1' – urema Commented May 20, 2011 at 9:54
  • 1 when you say "doesn't work" what does that mean? – Mic Commented May 20, 2011 at 9:54
 |  Show 11 more ments

3 Answers 3

Reset to default 1

First, a small introduction to how dropdowns work:

<select id="aspectResult">
    <option value="Option1">Option1</option>
</select>

To read the selected value from the dropdown, you should do this:

var dropdown = document.getElementById('aspectResult'),
selectedValue = dropdown.options[dropdown.selectedIndex].value;

Then, you create the <p> element with a text node inside:

var p = document.createElement('p'),
txt;

if (selectedValue == 'Option1') {
    txt = document.createTextNode('This is option 1');
}

Afterwards, you can append the newly created paragraph to the container of your choice:

var container = document.getElementById('mainBlock');

if (txt) {
    p.appendChild(txt);
    container.appendChild(p);
}

All together now!

If you are trying to add the ptext to the paragraph, you need to add two lines to the end:

function addAspect(aspect) {
    var prnt = document.createElement('p');
    var ptext;
    if( aspect == "Option1" ) {
        ptext = document.createTextNode("This is option1");
        prnt.appendChild(ptext); // Add the text to the paragraph
        document.body.appendChild(prnt); // Add the paragraph to the document
    }
}

Your function creates a text node but then does nothing with it, so you code appears to do nothing at all. You need to append the text node to an element somewhere in the DOM:

document.body.appendChild(ptext);

Your full code appears to be working fine in IE9, Firefox 4 and Chrome 11. See http://jsbin./ekura5/.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744406802a4572673.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信