2

Hey all Trying to generate a 1-button form that will go in a cell of a table. The catch is that this needs to be generated dynamically in javascript. The table has a couple other values. I did the table first without the form part and everything was working fine... Now the table doesn't generate at all.

I know the way I'm generating the form is incorrect but I'm not sure why.... Please help!

Thanks

function populateInventory() {
        clearTable();  //This works
        var artistIndex = byId('artist').selectedIndex;
        var albumIndex = byId('albumSelect').selectedIndex;
        var inventoryArray = inventoryNames[artistIndex][albumIndex];

        for (i = 0; i < inventoryArray.length; i++) {
            var idValue = inventoryArray[i][2];
            var conditionValue = inventoryArray[i][3];
            var priceValue = inventoryArray[i][4];

            var table = byId('table');
            var row = table.insertRow(i + 1);

            var submitCell = row.insertCell(0);
            var idCell = row.insertCell(1);
            var conditionCell = row.insertCell(2);
            var priceCell = row.insertCell(3);

//Begin problem?!
                var form = document.createElement("form");
                form.method = "post";
                form.action = "<?php echo $_SERVER[PHP_SELF];?>";
                var inventoryIdElement = document.createElement("<input name='inventoryIdElement' type='hidden' value='" + idValue + "' ></input>");
                form.appendChild(inventoryIdElement);
                var submitElement = document.createElement("<input name='submit' type='submit' value='Remove' ></input>");
                form.appendChild(submitElement);

            //End problem?!
            var idElement = document.createTextNode(idValue);               
            var conditionElement = document.createTextNode(conditionValue);                 
            var priceElement = document.createTextNode("$" + priceValue);               

            submitCell.appendChild(form);
            idCell.appendChild(idElement);
            conditionCell.appendChild(conditionElement);
            priceCell.appendChild(priceElement);
        }
    }

2 Answers 2

2

Have you considered using a good library for DOM manipulation, like jQuery, YUI, Prototype, Dojo, Ext, etc.? It would make your life much easier for this sort of things.

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

2 Comments

I have... however I need to get this finished quickly and don't have time to learn that. I will definitely move to something like this after this deadline. This is the final javascript piece missing..
In my opinion and experience; beginners tend to learn a library like jQuery much faster than vanilla javascript.
0

I don't think you are using createElement properly there. It takes just the tag name, not the whole markup.

2 Comments

changed it to be like this.. var inventoryIdElement = document.createElement("input"); inventoryIdElement.name = "inventoryIdElement"; inventoryIdElement.type = "hidden"; inventoryIdElement.value = idValue; form.appendChild(inventoryIdElement); Still fail... :(
The form action gets printed properly. This is in a php file.

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.