'Get value of editable <td> tags through onchange function?

I am trying to get the value of tag which is set editable, in Javascript. code given below

<td class="cent" contenteditable='true' id="value_per_visit" 
onchange="myFunction()"> -- </td>

Here is my javascript code

<script type="text/javascript">
function myFunction() {
  var x = document.getElementById("value_per_visit").value;
  document.getElementById("value_per_visit").innerHTML ="gotit";
  alert("asad");
}


</script>

But i am not able to reach the function. Is there any solution?



Solution 1:[1]

The best way is to make an input inside an td element:

<table>
<tr>
  <td>
    <input  id="Test" value="0" />
  </td>
</tr>
</table>

and bind a change event on it:

$('#Test').change(function(){
    alert($(this).val());
})

Solution 2:[2]

use onblur instead of onchange works for me!

--

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 arturas
Solution 2 wei