Pravesh Singh
Pravesh Singh

Reputation: 324

Water mark textbox using HTML and jQuery

I want to make a water mark textbox using HTML and jQuery but problem is when i go to another page and back on again water mark textbox page the water mark text is show in normal text.

Below is my jQuery Code:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
<script type="text/javascript">

    (function ($) {
        $.fn.extend({
            inputWatermark: function () {
                return this.each(function () {
                    // retrieve the value of the ‘placeholder’ attribute
                    var watermarkText = $(this).attr('placeholder');
                    var $this = $(this);
                    if ($this.val() === '') {
                        $this.val(watermarkText);
                        // give the watermark a translucent look
                        $this.css({ 'opacity': '0.65' });
                    }



                    $this.blur(function () {
                        if ($this.val() === '') {
                            // If the text is empty put the watermark
                            // back

                            $this.val(watermarkText);
                            // give the watermark a translucent look
                            $this.css({ 'opacity': '0.65' });
                        }
                    });



                    $this.focus(function () {
                        if ($this.val() === watermarkText) {
                            $this.val('');
                            $this.css({ 'opacity': '1.0' });
                        }
                    });
                });
            }
        });

    })(jQuery);
</script>

Here is my HTML Code:

<body>
<input id="input1" placeholder="Placeholder here..." />
<input id="input2" placeholder="2nd Placeholder" />
<a href="../../back.htm">google</a>
<script type="text/javascript">

    $(document).ready(function () {
        $(':input').inputWatermark();
    });

</script></body>

Upvotes: 0

Views: 2920

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

try changing:

if ($this.val() === '') {

to

var txtVal = $.trim( $this.val() );
if ( txtVal === '') {

add color, like

...
$this.css({ 'opacity': '0.65' });
$this.css({ 'color': '#a8a8a8' });

Upvotes: 1

Related Questions