udexter
udexter

Reputation: 2347

Selecting next submit after actual textarea and disable it

I have various instances of a form in a page. I want to desactivate the submit button of that ones with the default text in the textarea. My actual code is:

// This works
$(".proposal-write-comment-submit").each(function(){
    var element = $(this).closest(".proposal-write-comment-textarea");

    var default_text = element.attr("data-default");
    var value_text   = element.val();

    if(default_text == value_text) {
        $(this).attr("disabled","disabled");    
    }
});

// This no
$(".proposal-write-comment-textarea").keypress(function(e){
    var default_text = $(this).attr("data-default");
    var value_text   = $(this).val();

    if(default_text == value_text) {
        $(this).closest(".proposal-write-comment-submit").attr("disabled","disabled");  
    } else {
        $(this).closest(".proposal-write-comment-submit").removeAttr("disabled");
    }
});

My HTML code:

<form class="proposal-write-comment" method="post" action="">
    <textarea class="proposal-write-comment-textarea" data-default="Write a comment...">Write a comment...</textarea>
    <input class="proposal-write-comment-submit" type="submit" value="Save" />
</form>

The second part of the javascript doesn't works. How I can select right the submit buttons "next to" the textarea?

Thank you in advance!

Upvotes: 0

Views: 129

Answers (2)

ipr101
ipr101

Reputation: 24236

You could try the 'siblings' selector -

if(default_text == value_text) {
        $(this).siblings(".proposal-write-comment-submit").attr("disabled","disabled");  
    } else {
        $(this).siblings(".proposal-write-comment-submit").removeAttr("disabled");
    }

Upvotes: 1

James Allardice
James Allardice

Reputation: 166001

closest looks up the DOM tree, at parents and ancestors, rather than at siblings. As the input element is a sibling of the textarea element, you need to look at siblings. To do that, you could use next, (assuming the input always directly follows the textarea... if not, you could use nextAll or siblings with the appropriate selector)

$(this).next().removeAttr("disabled");

Upvotes: 0

Related Questions