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

2 posts, 0 answers
  1. Stewart
    Stewart avatar
    37 posts
    Member since:
    Oct 2013

    Posted 11 Nov 2013 Link to this post


    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%">
        <div class="buttonArea">
            <a  id="postcomment"
            data-bind="click: onPostComment"
            style="background-color: #f0f0f0; colour: green">Post</a>

    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.

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

    Any pointers, please?

    Thanks, Stewart :)
  2. Steve
    Steve avatar
    10941 posts

    Posted 12 Nov 2013 Link to this post

    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?

    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.
Back to Top