Reputation: 54
I have a textarea that I would send the changes to a database. However I don't understand or find how to call javascript function when onchange is activated. I've tried this first :
<div class= 'boiteContent'>
<textarea id='id_textarea' onchange='update(this)' >" . [PHP code] . "</textarea>
</div>
...
function update(textarea){
var name_field = $this.attr('id');
var value_field = $(this).text();
if (value_field.length >400){
alert('La taille du champs ne doit pas excéder les 400 caractères');
location.reload();
}
else{
$.ajax({
url: '/Projectmanager/updatedashboard',
type: 'post',
data: {
namefield :name_field,
fieldValue: value_field ,
id_project : <?php echo $this->currentP->getId_project()?>
},
});
}
}
I tried multiple ways , and the best, but still not working, is the following :
My fiddle of the textarea Problem.
Could anyone help me having this javascript function executed when onchanges ?
I have to precise that I must pass in parameter the textarea objet to get the ID and the text() of it.
Thanks for your answers !
Faya.
Upvotes: 0
Views: 85
Reputation: 54
This is what finally works. Thank you !
In html :
<textarea id='description_project' onchange="updateTextAreaProject(this)" >
In script :
var name_field = textarea.id;
var value_field = textarea.value;
Upvotes: 1
Reputation: 18888
You appear to be referring to the textarea name and value incorrectly. Change:
var name_field = $this.attr('id');
var value_field = $(this).text();
To:
var name_field = textarea.name;
var value_field = textarea.value;
I haven't the foggiest clue where the $this
variable is coming from. Also, the this
variable will refer to the window
object.
Upvotes: 1