PDA

View Full Version : Get value of input element inside tbody


thereal88
09-12-2006, 04:57 PM
I have something like this:

<table>
<form name=searchform>
<tr><td><input type=text name=notimportant></td></tr>
<tbody>
<tr><td><input type=text name=thefield></td></tr>
</tbody>
</table>
</form>

I need to get the value of thefield using JavaScript. I tried this:
alert(document.searchform.thefield.value)
but it doesn't work. However,
alert(document.searchform.notimportant.value)
will give me the value of the field named 'notimportant', which is not inside the <tbody> element. I'm guessing it has to do with the <tbody>. I can't get rid of it because I need it to add rows to the table, also using JavaScript.

Does anyone know how to get the value of 'thefield'?
Or otherwise, how to add rows in the middle of a table (between other rows), without a <tbody>?

Kravvitz
09-13-2006, 12:49 AM
You're using an illegal nesting. <form> may not be a direct child of several types of elements including <table>, <tbody>, and <tr>.

It's bad practice to not quote HTML attribute values.

Does that fix it?

thereal88
09-13-2006, 06:57 AM
Wow, it does fix it. I moved the <form> starting tag up one line, above the <table> starting tag and now it works! Strange because I've never had any problems with it before. So thanks.

And about the quotes in the HTML attributes, it doesn't give any problems until I use a value that has spaces or interpunction in it. So if not necessary, I don't use quotes because it's easier to echo() with PHP (otherwise I need to insert a \" ).

Kravvitz
09-13-2006, 08:20 AM
You're surprised that fixing invalid HTML makes JavaScript work as expected?

I can't force you to use the quotes all of the time, but keep in mind that XHTML always requires the quotes around the attribute values.

thereal88
09-13-2006, 08:39 AM
Well, XHTML is not HTML.
And no, I'm not surprised fixing illegal HTML fixed the problem, I'm surprised I've never had any problems with it before.

Kravvitz
09-13-2006, 08:55 AM
Well, XHTML is not HTML.
True. I'm glad you know there's a difference. Many people don't seem so aware.

And no, I'm not surprised fixing illegal HTML fixed the problem, I'm surprised I've never had any problems with it before.
*shrugs*

_Aerospace_Eng_
09-13-2006, 11:27 AM
By the way you don't have to escape quotes in an echo if you begin the echo with a single quote
echo '<input type="text" name="notimportant">';

thereal88
09-13-2006, 11:30 AM
Yeah that's true. Maybe I'll try to use them more often.