Page 1 of 1

Append a SPAN to all input type="text" fields

Posted: Fri Feb 12, 2010 2:24 pm
by icesolid
Is there a way to onload, append a span tag to to the end of all input fields that are equal to type text.

ex: <input type="text"><span></span>

All text inputs will have that span tag added to the end of it on page load.

Re: Append a SPAN to all input type="text" fields

Posted: Fri Feb 12, 2010 2:30 pm
by Darhazer
It's really easy with a JavaScript framework like jQuery:

Code: Select all

 
$(document).ready(function(){
    $('input[type="text"]').each(function(){
        $(this).append('<span> ...</span>');
    });
});
 
code not tested ;)

It's possible with pure javascript of course, you can get all input elements via document.getElementsByTagName() and then in a loop check their type and append the span element.

Re: Append a SPAN to all input type="text" fields

Posted: Fri Feb 12, 2010 2:34 pm
by icesolid
I do run jQuery and love it. However that does not work with it?

Re: Append a SPAN to all input type="text" fields

Posted: Fri Feb 12, 2010 8:40 pm
by daedalus__
he said he didn't test the code. it may need adjustments

Re: Append a SPAN to all input type="text" fields

Posted: Sat Feb 13, 2010 1:38 am
by Darhazer
icesolid wrote:I do run jQuery and love it. However that does not work with it?
Well, the code above produces invalid HTML - <input type="text"><span/></input>
The correct code, and this time it's tested, is:

Code: Select all

$(document).ready(function(){
     $('input[type="text"]').each(function(){
         $(this).after('<span> ...</span>');
     });
 });