简体   繁体   中英

How can I getting textarea value using javascript

I want to make submit event depend textarea value. cause i want to check textarea is vaild. so when I was using javascript function getElementByid, it result was placeholder value.

I just want to know how can I fix it.

<div class="clearfix">
    <div class="input">
        <textarea name="comment" id="comment" placeholder="<?php _e("Your Comment Here", "bonestheme"); ?>" tabindex="4" style="border:1px solid #cbcbcb; resize:none"></textarea>
    </div>
</div>

<div class="form-actions">
  <input class="btn-commnet" name="submit" type="submit" id="submit" tabindex="5" value="<?php _e("Submit Comment","bonestheme"); ?>" />
</div>

<?php endif; ?>

You can use .value . Here's a demo:

 var textarea = document.getElementById('test'); var result = document.getElementById('result'); function updateResult() { result.textContent = textarea.value; } textarea.addEventListener('keyup', updateResult);
 <textarea id="test" placeholder="Some placeholder"></textarea> <p id="result"></p>

A little unclear as to what you need to do but if you just want to validate that the textarea value is something other then the placeholder text, you could add a handler to call on click of the button, something like:

<input class="btn-commnet" name="submit" type="submit" id="submit" tabindex="5" value="<?php _e("Submit Comment","bonestheme"); ?>" onclick="return ValidationEvent()"/>

then have a javascript function that checks the value prior to submitting..

function ValidationEvent() {
    commentElm = document.getElementById('comment');
    if(commentElm.value === commentElm.placeholder)
        return false;   
    return true;
}

This could be greatly simplified using Jquery.

Try below example. It's very easy. It doesn't return placeholder value it returns content that is written in textarea either you write static or dynamic. I just specify how to get simply textarea value instead of placeholder. If you want to set any validation then show me your java script code, so that i can give you better result.

<html>
<head>
<script type="text/javascript">
function abc()
{
  var getTextArea = document.getElementById("txtArea");
  var value = getTextArea.value;
  alert(value);//It will show you text area value
}
</script>
</head>
<body>
<textarea id="txtArea" placeholder="hello">
</textarea>
<input type="submit" onclick="abc()"/>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM