1

I have create a jquery to convert title attribute of input to span with class "field_title". when I click on input though span class hide but input field remains disabled. Issue is I can't enable the input field.

<div class="field">
  <input type="text" name="email" class="w_def_text" title="Email*" />
  <img class="error" width="27" height="27" src="images/error.jpg" alt="Error"> 
</div>

<script>
function init_fields() {    
    $('.w_def_text').each(function() {
        var text = $(this).attr('title');
        var html = '<span class="field_title">' + text + '</span>';
        $(this).parent().append(html);

        if($(this).val() == '') {
            $(this).hide();
            $(this).next().show();
        }
        else {
            $(this).css({'display' : 'block'});
            $(this).next().hide();
        }
    });

    $('.w_def_text').live('blur', function() {
        if($(this).val() == '') {
            $(this).hide();
            $(this).next().show();
        }
    });

    $('.w_def_text ~ span').live('click', function() {
        $(this).hide();
        $(this).prev().css({'display' : 'block'}).focus();
    });
}
</script>
3
  • I'm not sure what exactly the issue is, but I think it might be solved to an extent by using label tags instead of spans for the title. More semantic as well, makes sense without a style. Also have the added benefit of setting focus on the associated input field when clicked. Commented Oct 27, 2011 at 18:54
  • Too difficult to decipher what you are wanting. Can you give us more to work with or use this fiddle (jsfiddle.net/VwPM2) to help us a bit more. Commented Oct 27, 2011 at 19:02
  • @ swatkins : Exactly when I click on Email* that is span I want input field here... Commented Oct 27, 2011 at 19:10

2 Answers 2

1
$('.w_def_text ~ span').live('click', function() {
    $(this).hide();
    $(this).prev().prev().css({
        'display': 'block'
    }).focus();
});

You need .prev().prev() because that's the input. And in second function

$('.w_def_text').live('blur', function() {
    if($(this).val() == '') {
        $(this).hide();
        $(this).next().next().show();
    }
});

you need .next().next() or something like that depending on what you want. You should understand what is wrong now. :)

Sign up to request clarification or add additional context in comments.

3 Comments

I want bit modification. on blur if input is empty than class="error" should be visible. otherwise on load it should be hidden.
I did it. Thanks mate. I can't make your reputation +100 otherwise I did it for sure.... :)
No problem. By the way: have you considered using placeholder attribute in input? Note that this is HTML5.
1

I think instead of:

$(this).parent().append(html);

you want to do:

$(this).after(html);

And perhaps be more explicit, instead of just .next().show() - specify the sibling class you want to target:

$(this).next('.error').show()
...
$(this).next('.error').hide()

2 Comments

Great I got result wat I want but still on blur my error class is not shown if field is null.
It's quite difficult to understand the requirement, but perhaps using $(this).next('.error').show() and $(this).next('.error').hide() will get the result you want?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.