Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; ?>
like image 607
Yuri Lee Avatar asked May 08 '26 04:05

Yuri Lee


2 Answers

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>
like image 94
rnevius Avatar answered May 10 '26 18:05

rnevius


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.

like image 42
Goat Avatar answered May 10 '26 16:05

Goat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!