This is a migrated thread and some comments may be shown as answers.

Odd textarea issue - maintains value even when cleared with .val();

1 Answer 15 Views
General Discussion
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Stewart
Top achievements
Rank 1
Stewart asked on 11 Nov 2013, 06:32 PM
Hello,

A slightly odd one here!

I have a textarea to allow the user to submit a comment. Once a comment is submitted, I use JavaScript to confirm the comment with an alert on screen and use $('#newComment').val(''); to empty the textarea so that another comment can be made. Here is the form code:

<div id="commentBox">
    <div style="text-align: left;">Your comment:</div>
    <textarea data-bind="value: comment"
                id="newComment" rows="3"
                style="background-color: #FFFFFF; border-style: solid; border-width: 1px; width: 98%">
    </textarea>
    <div class="buttonArea">
        <a  id="postcomment"
        data-role="button"
        data-bind="click: onPostComment"
        class="login-button"
        style="background-color: #f0f0f0; colour: green">Post</a>
    </div>
</div>

However - although after the first comment is made, if without navigating away the user just hits the post button again without entering any more text, then the text from the first comment is posted.

I've proved that with this code, where both on initial submission and also second time - with an APPARENTLY empty text area - the first alert says "undefined" but the second alert contains the text from the first comment.

alert(comment);
  
var that = this,
    comment = that.get('comment').trim();
  
    alert(comment);

Any pointers, please?

Thanks, Stewart :)

1 Answer, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 12 Nov 2013, 04:41 PM
Hello Stewart,

Could it be the notorious id selector vs. class selector issue discussed in another thread? Is that the only and first instance of that textarea? Also have you tried using $('#newComment').text(''); as well?

Regards,
Steve
Telerik
You've missed the Icenium Visual Studio Integration keynote? It has been recorded and posted here.
Looking for tips & tricks directly from the Icenium team? Check out our blog!
Share feedback and vote for features on our Feedback Portal.
Tags
General Discussion
Asked by
Stewart
Top achievements
Rank 1
Answers by
Steve
Telerik team
Share this question
or