JQuery show submit button on input field click

I am trying to create a comment input field that will show a submit button on a dynamically generated form when you click on the input field. Similar to how facebook comments work. When you click on the input field, the submit button appears and when you click on it, it disappears. All comment input is comment_1, etc., and the submit button id is submit_1, etc.

I tried this,

jQuery("#[id^='comment_']").live('click',function(event){ 
    if(jQuery("#[id^='comment_']").val() == ""){ 
        jQuery("#[id^='submit_']").hide(); 
    } 
    else { 
        jQuery("#[id^='submit_']").show(); 
    } 
}); 

      

And for some reason it won't work. Any suggestion or how it can be done would be great.

+2


a source to share


2 answers


jQuery("[id^='comment_']").live('focusin focusout',function(e){
    var commentText = "Write a comment...",
        id = this.id.replace('comment_',''),
        val = jQuery(this).val();   
    if (e.type == 'focusin'){
        val = (val == commentText) ? '' : val; 
        jQuery("#submit_"+id).show();
    } else if (e.type == 'focusout') {
        val = (val == '') ? commentText : val; 
        if( val == commentText){ 
            jQuery("#submit_"+id).hide(); 
        }
    }
    jQuery(this).val(val);
}).trigger('focusout');

      



+1


a source


You need to remove #

from selectors. I also think that you do not need the event click

, but focus

and blur

.



+2


a source







All Articles