How to add id to input?
<p>My CMS is very old. </p>

<p>It gives the following HTML.
I want to add id to each input and id name which is a text from the previous td, such as navn, e-po, hjem etc.
Id can be 4 letters.</p>

<p>I tried this code, but it gives only id="----" to each input.</p>

<pre><code>&lt;script type="text/javascript"&gt;
$(document).ready(function(){
$('table tr').each(function(){
var formid=$('td:first', this).text().toLowerCase().slice(0,4);
$('input', this).attr('id', function() {
return formid});     
});
});
&lt;/script&gt;
</code></pre>

<p>Basically what I tried to do is that. For each tr. set a variable formid to the first td and get the text and change to lowercase with four letters.
And find this input and add id of this formid.</p>

<p>(UPDATE)
After two replies I tried the followings.</p>

<pre><code>$(document).ready(function() { 
$('table input').each(function() { 
var formid=$(this).parents('td').prev().text().toLowerCase();
alert (formid);
});
});
</code></pre>

<p>This alerts navn:, e-post:, hjemmeside: and sak:.</p>

<p>So far so good.
But when I use the following, it alerts nothing. I want to take out: from each string.</p>

<pre><code>$(document).ready(function() { 
$('table input').each(function() { 
var formid=$(this).parents('td').prev().text().toLowerCase();
var inputid=formid.slice(0,5);
alert (inputid);
});
});
</code></pre>

<p>Can anyone help me please?</p>

<p>Thanks in advance.</p>

<pre><code>&lt;TABLE CELLSPACING="0" CELLPADDING="2" BORDER="0"&gt;
&lt;TR&gt;
&lt;TD&gt; Navn: &lt;/TD&gt;
&lt;TD&gt;&lt;INPUT TYPE="text" CLASS="input-style" NAME="Namn" VALUE="" MAXLENGTH="50"&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt; E-post: &lt;/TD&gt;
&lt;TD&gt;&lt;INPUT TYPE="text" CLASS="input-style" NAME="Epost" VALUE="" MAXLENGTH="50"&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt; Hjemmeside: &lt;/TD&gt;
&lt;TD&gt;    &lt;INPUT TYPE="text" CLASS="input-style" NAME="Hemsida" VALUE="http://"  MAXLENGTH="50"&gt;
&lt;/TD&gt;
&lt;/TR&gt;

&lt;TR&gt;
&lt;TD&gt; Sak: &lt;/TD&gt;
&lt;TD&gt;&lt;INPUT TYPE="text" CLASS="input-style" NAME="Arende" VALUE="" MAXLENGTH="50"&gt;
&lt;/TD&gt;
&lt;/TR&gt;

&lt;TR&gt;
&lt;TD&gt; Telefon:
&lt;/TD&gt;
&lt;TD&gt;&lt;INPUT TYPE="text" CLASS="input-style" NAME="Telefon" VALUE="" MAXLENGTH="50"&gt;
&lt;/TD&gt;
&lt;/TR&gt;

&lt;TR&gt;
&lt;TD&gt;
Tekst:
&lt;/TD&gt;
&lt;TD&gt;
&lt;TEXTAREA NAME="Innehall" ROWS="5" COLS="42"&gt;&lt;/TEXTAREA&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;

....
....
....


&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
</code></pre>
