Jump to content

Recommended Posts

Hi everyone, i got a simple Javascript problem id like some help with.

 

I am very VERY new at javascript and probably dont know all the syntax yet but im hopin you can help me solve it or find a better solution.

Ive put up an exercise for myself to learn better, in this case to use form inputs to create a table with javascript. Progress is slow and so far ive tried to find the problem myself, but i cant seem to find the solution for it.

 

this is the code:

<html>
<head>
<title>Tabell Skapare</title>
<script type="text/javascript" language="Javascript">

function createTable()
{
    var formInputs = document.forms["Controls"].elements;
    document.write(document.formInputs.elements["farg"].value);
    document.write(document.formInputs.elements["cell1"].value);
    document.write(document.formInputs.elements["cell2"].value);
}

</script>
</head>
<body>
<div id="Controls">
<form name="Controls">
Skriv en bakrundsfärg i färgkod: <input type="text" name="farg">
<br />
Skriv text i den första tabellcellen: <input type="text" name="cell1">
<br />
Skriv text i den andra tabellcellen: <input type="text" name="cell2">
<br />
<input type="button" value="Create" onclick="createTable()">
</form>
</div>
<div id="Table">
<p id="tableID"></p>
</div>
</body>
</html>

 

Please ignore the other language, that isnt important :P

 

When i click the button, nothing happens. At this stage, it is just supposed to show the values of the different fields so i know that the values are displayed in javascript correctly. Hopefully, its just me typing the code incorrectly.

 

Hope you can help me with is.

 

Entiranz.

The line with formInputs is fine, but the stuff after it isn't.

1. formInputs is a variable. It is not located inside document so document.formInputs won't work. Just call it by its name.

2. formInputs is already the elements collection. You don't need another .elements[] on it.

ok, that worked, thanks alot. Now i know that the values are assigned properly. now i have another problem though. :-(

 

Now im trying to create a table with the main function, and as far as i can tell i have probably used the HTML tags inside the wrong way. Actually, im just assuming that u can use them inside, im unsure. I know however now that you cant use them like you do in php, because then this would probably have worked.

 

This is the current code now:

<html>
<head>
<title>Tabell Skapare</title>
<script type="text/javascript" language="Javascript">

function createTable()
{
    var formInputs = document.forms["Controls"].elements;
    document.write("<html><body><table bgcolor=formInputs["farg"].value>")
    document.write("<tr><td>formInputs["cell1"].value</td><td>formInputs["cell2"].value</td></tr>");
    document.write("</table></body></html>");
}

</script>
</head>
<body>
<div id="Controls">
<form name="Controls">
Skriv en bakrundsfärg i färgkod: <input type="text" name="farg">
<br />
Skriv text i den första tabellcellen: <input type="text" name="cell1">
<br />
Skriv text i den andra tabellcellen: <input type="text" name="cell2">
<br />
<input type="button" value="Create" onclick="createTable()">
</form>
</div>
<div id="Table">
<p id="tableID"></p>
</div>
</body>
</html>

 

Once again, i hope you guys can help me correct my numerous errors.

 

Entiranz.

It's the quoting you have on those HTML strings, not the HTML itself. PHP allows you to embed variables in strings because it can use the "$" to recognize there's a variable. JavaScript has no such feature.

 

To put a value into a string you have to stop the string, add the value, and begin the string again.

"</pre>
<table bgcolor=" + formInputs[" farg>"</

 

There are a couple other things I should point out now (there are a couple things that aren't as important to mention yet):

- Don't use the language attribute on script tags. Using the type is enough.

- document.write() will do weird things. If you want to create the table inside the div#table (because it shouldn't go inside the p#tableID) then actually put it in there; the cheap way to do that is using .innerHTML:

var table = "</pre>
<table bgcolor=" + formInputs[" farg>...</table>";<br>document.getElementById("table").innerHTML = 

- bgcolor is deprecated. Use CSS's background(-color) instead.

Thank you, ive gotten it to work perfectly, it now displays the table i want. :D

Probably couldnt have done this without you.

 

On a side note though, just a simple question regarding the background color. How exactly do you use the CSS for that in here, and where? Since im used to CSS inside other files or in a style tag, how would you implement it inside the javascript bit, (i tried a copy and a few variations of the example you gave me, it failed)?

 

Entiranz.

On a side note though, just a simple question regarding the background color. How exactly do you use the CSS for that in here, and where? Since im used to CSS inside other files or in a style tag, how would you implement it inside the javascript bit, (i tried a copy and a few variations of the example you gave me, it failed)?

You can do it inline. Most of the time you should use classes and selectors to get what you need, such as naming the table with class="controle", but occasionally it makes sense to do a bit of styling inline with the element. Like now.

"</pre>
<table style="'background:" forminputs>"</

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.