I am new to JavaScript so please be understanding of my situation.
I have created a simple HTML form which includes a drop down menu (to select quantity of item) and a submit button (to add to a cart).
Once submitted the results of the form are displayed in a table on the webpage.
So far with my code I can display the results of the form options.
However, I need to replace the quantity with the variable result for "qtytoy1".
Here is my code.
function addToy1New()
{
var i = document.getElementById("qty_toy1");
var qtytoy1 = i.options[i.selectedIndex].text;
var htmlTagString="<table width=\"100%\" border=\"0\">"
+" <tr>"
+" <td>Optimus Prime</td>"
+" <td> x 1</td>"
+" <td>$26.99</td>"
+" </tr>"
+" <tr>"
+" <td></td>"
+" <td>Total:</td>"
+" <td>$26.99</td>"
+" </tr>"
+"</table>";
document.getElementById("toy1_add").innerHTML=htmlTagString;
document.getElementById("toy1_test").innerHTML=qtytoy1;
}
How can I replace "$26.99" with my variable "qtytoy1"?
I am pretty sure there are better ways around this method. Please share if you think so but make sure it is understandable for a novice such as myself. :)
htmlTagString
, there was also the possibility that #toy1_add
already had that kind of html and need only that one td
replaced, in that case jQuery
would be quite handy. But, yes jQuery template is also a good point - d_inevitable 2012-04-05 00:29
var htmlTagString="<table width=\"100%\" border=\"0\">"
+" <tr>"
+" <td>Optimus Prime</td>"
+" <td> x 1</td>"
+" <td>" + qtytoy1 + "</td>"
+" </tr>"
+" <tr>"
+" <td></td>"
+" <td>Total:</td>"
+" <td>" + qtytoy1 + "</td>"
+" </tr>"
+"</table>";
You should be able to use the replace
method on your string:
htmlTagString = htmlTagString.replace("$26.99", qtytoy1);
Then go on from there.