2

I am working on an alert button that gets generated depending on a select drop-down. The JavaScript has been able to read and send the values of the <select> drop-down exactly as I want it to. But, when it comes to displaying the button that triggers the alert message, is my problem.

ISSUES

-- I am not too sure if I am actually removing the child correctly (syntax issue)

-- I can't seem to get the button to display. Instead, it only performs its onclick command.

JAVASCRIPT

function displayInfo(key) {
    alert(key);
}

function determineDisplay(Item, ButtonLocation) {
    // Get selected value from drop down
    var si = Item.selectedIndex;
    var sv = Item.options[si].value;

    // Create View Button to display info
    var VB = document.createElement("input");

    // Assign attributes to button
    VB.setAttribute("type", "button");
    VB.setAttribute("value", "view");
    VB.setAttribute("display", "inline-block");
    VB.onClick = displayInfo(sv);

    // Insert button
    var insertHere = document.getElementsById(ButtonLocation);
    var CheckChild = insertHere.lastChild;
    if(lastChild) {
        insertHere.removeChild(CheckChild);
        insertHere.appendChild(VB);
    }
    else
        insertHere.appendChild(VB);
 }

HTML

<div id="SoupBox">
    <select id="Soup" name="Soup" onchange="determineDisplay(this, 'contentView_Two');">
        <option>--Select--</option>
<?php foreach($soup_products as $key => $product) :
$name = $product['name']; $cost = number_format($product['cost'], 2);
$item = $name . ' ($' . $cost . ')';
?>
        <option value="<?php echo $key; ?>"><?php echo $item; ?></option>
<?php endforeach; ?>
    </select>
    <input name="s_qty" type="text" placeholder="qty" size="1" maxlength="1" />
    <label id="contentView_Two"></label>

</div>

HTML WITHOUT PHP

<div id="SoupBox">
    <select id="Soup" name="Soup" onchange="determineDisplay(this, 'contentView_Two');">
        <option>--Select--</option>
        <option value="CN-CF">Healthy Choice Hearty Chicken ($1.31)</option>
        <option value="CN-CT">Pacific Foods Organic Creamy Tomato ($3.75)</option>
        <option value="CN-LT">Amy's Organic Lentil Vegetables ($2.62)</option>
        <option value="ND-BF">Ramen beef Noddles ($0.50)</option>
        <option value="ND-CF">Ramen Chicken Noddles ($0.50)</option>
        <option value="ND-LS">Ramen Lime Shrimp Noddles ($0.50)</option>
        <option value="ND-SF">Ramen Shrimp Noddles ($0.50)</option>
    </select>
    <input name="s_qty" type="text" placeholder="qty" size="1" maxlength="1" />
    <label id="contentView_Two"></label>

</div>
5
  • Can you show us the rendered HTML (without the PHP code) Commented May 4, 2013 at 20:58
  • @ExplosionPills sure, give me a sec. Commented May 4, 2013 at 20:59
  • @ExplosionPills posted it. Commented May 4, 2013 at 21:01
  • jsfiddle.net/ExplosionPIlls/fyH4W -- is that fixed? Commented May 4, 2013 at 21:02
  • I can't located your modifications in the code. And also the alert massage happens before the button display. Commented May 4, 2013 at 21:10

2 Answers 2

1

Different problems here:

  1. VB.setAttribute("display", "inline-block"): this is wrong, because the display attribute is from the style of the element, not of the element itself. Use VB.style.display instead.

  2. VB.onClick = displayInfo(sv): this is wrong, because what you're doing here is executing the function right away. You probably meant VB.onclick = function() { displayInfo(sv); }

Also, you could try adding the following styles attributes:

VB.style.position = 'absolute';
VB.style.left = '5px';
VB.style.top = '5px';
VB.style.width = '50px';
VB.style.height = '20px';

Otherwise I'm not sure how js would render it.

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

5 Comments

VB.style.display = "inline-block"
new lines VS.style.display = "inline-block"; VB.onClick = function() {displayInfo(sv);}; but it doesn't seem to fix the issue. @Sebas I also tried the 3. suggestion you put and that didn't do the trick either.
@Sebas I've found this online text-editor link and it says there is a missing semicolon on the code. Could post your 3rd suggestion again so I could look up the handler's syntax.
ahh Never mind, fixed it, VB.onClick = function(p) { return function(){ displayInfo(p);};}(sv);
yeah that 3rd point was not so important. Does the big picture works now?
0

fixed it, thank you all for the help.

JavaScript

function displayInfo(key) {
    alert(key);
}

function determineDisplay(Item, ButtonLocation) {
    // Get selected value from drop down
    var si = Item.selectedIndex;
    var sv = Item.options[si].value;

    // Create View Button to display info
    var VB = document.createElement("input");

    // Assign attributes to button
    VB.setAttribute("type", "button");
    VB.setAttribute("value", "view");
    VB.setAttribute("display", "inline-block");
    VB.onclick = function() { displayInfo(sv);};

    // Insert button
    var insertHere = document.getElementById(ButtonLocation);
    var CheckChild = insertHere.lastChild;
    if (CheckChild) {
        insertHere.removeChild(CheckChild);
        insertHere.appendChild(VB);
    }
    else
        insertHere.appendChild(VB);
 }

HTML WITHOUT PHP

<div id="SoupBox">
                <select id="Soup" name="Soup" onchange="determineDisplay(this, 'contentView_Two');">
                    <option>--Select--</option>
                                            <option value="CN-CF">Healthy Choice Hearty Chicken ($1.31)</option>
                                            <option value="CN-CT">Pacific Foods Organic Creamy Tomato ($3.75)</option>
                                            <option value="CN-LT">Amy's Organic Lentil Vegetables ($2.62)</option>
                                            <option value="ND-BF">Ramen beef Noddles ($0.50)</option>
                                            <option value="ND-CF">Ramen Chicken Noddles ($0.50)</option>
                                            <option value="ND-LS">Ramen Lime Shrimp Noddles ($0.50)</option>
                                            <option value="ND-SF">Ramen Shrimp Noddles ($0.50)</option>
                                        </select>
                <input name="s_qty" type="text" placeholder="qty" size="1" maxlength="1" />
                <label id="contentView_Two"></label>

                </div>

Comments

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.