views:

109

answers:

4

Hi, I need to show an image next to about 6 textboxes...I dont want to use div tags as these vary in position with IE/firefox etc. Is there a simple way to display a small image next to a textbox? such as using :after in css?

Cheers

+1  A: 

With jQuery is easy

$(".textbox").after('<img src="validation-mark.jpg" />');

Here the HTML:

<input type="text" class="textbox"></input>

UPDATE

If you want to show/hide the validation marks, maybe you can have them all declared in the HTML next to you text boxes like this

<input type="text" id="textbox1"></input><img src="validation-mark.jpg" />
<input type="text" id="textbox2"></input><img src="validation-mark.jpg" />
<input type="text" id="textbox3"></input><img src="validation-mark.jpg" />

And show them or hide them with this jQuery code:

$("#textbox1+img").hide();
$("#textbox1+img").show();

The jQuery selector #textbox1+img means "the image after the element with ID: textbox1"

victor hugo
Thanks that is easier than I thought!how would I hide/show the image in javascript?
Elliott
I put this just to answer the question but I recommend to use divs, Blaenk suggestion is pretty good to solve your browser compatibility issues
victor hugo
thanks...could you give me an example how to use the hide function within javascript? I havent used jquery before..
Elliott
Oh, this solution just work using jquery. It's really easy to learn and I'm sure you'd love it. Check out 'getting started with jquery' http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery
victor hugo
A: 

What do you mean by div's vary in position on each browser? If you're referring to padding and the like, you can try using a reset stylesheet.

Jorge Israel Peña
such as the image displays in-line with textbox in firefox, but in IE it shows about 3 lines below.
Elliott
Yeah, I'm pretty sure a reset stylesheet will solve a few of those spacing inconsistencies between browsers.
Jorge Israel Peña
+1  A: 

The style:

label.tbimg {
    display: inline-block;
    background-image: url(http://sstatic.net/so/img/vote-accepted-on.png); center right no-repeat;
    padding-right: 30px;   /*icon width*/
}
label.noimg {
    display: inline-block;
    padding-right: 30px;   /*icon width*/
}

The HTML:

<label class="tbimg"><input type="text" class="textbox"></input></label>
<label class="noimg"><input type="text" class="textbox"></input></label>
<label class="tbimg"><input type="text" class="textbox"></input></label>
<label class="tbimg"><input type="text" class="textbox"></input></label>
<label class="noimg"><input type="text" class="textbox"></input></label>
<label class="tbimg"><input type="text" class="textbox"></input></label>
o.k.w
A: 

Why not placing it inside a td?(I guess this is the simplest)

e.g.

<table>
<tr>
<td><INPUT TYPE="TEXT" id="t1"></td>
<td><INPUT TYPE="TEXT" id="t2"></td>
<td><INPUT TYPE="TEXT" id="t3"></td>
<td><INPUT TYPE="TEXT" id="t4"></td>
<td><INPUT TYPE="TEXT" id="t5"></td>
<td><INPUT TYPE="TEXT" id="t6"></td>
<td><img src="myimg.jpg" /></td>
</tr>
</table>

If needed (if the requirement is like that), create that at runtime.

priyanka.sarkar