Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - get .html() after having set with .html()

I'm using jQuery with the power of localStorage.

Here is what I have:

<textarea name="localStorageString" id="localStorageString"></textarea>
<script>
  var userJson = localStorage.getItem('userJson');
  $('#localStorageString').html(userJson);
  $('#localStorageString').keyup(function(){
    alert($(this).html());
});

The textarea is correctly filled, the problem is when I modify it, it always alerts the same value (set by html(userJson)).

Any idea ?

like image 275
Max13 Avatar asked Jan 27 '26 01:01

Max13


2 Answers

You need $(this).val() to get the value of a form element.

like image 55
SLaks Avatar answered Jan 28 '26 13:01

SLaks


I forgot this one. Why .html() doesn't work ?

html() - it's the native JavaScript .innerHTML function. It takes everything (all the nodes) inside the tag as a string.

<div id="Node">
   <a href="#">Hello</a>
</div>
console.info($('#Node').html()); // <a href="#">Hello</a>

val() - that's form.element.value.


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!