Reputation: 73
Hello i want to replace the text in div when user click on it i have tried this code but there is something wrong with my code
<script>
$(document).ready(function(){
//alert('hi');
$('div').append('hi')
$('div').click(function(){
$('this').append('how r u');
})
})
</script>
please help
Thanks
Upvotes: 0
Views: 105
Reputation: 33865
You are close!
this
is a keyword in JavaScript, but you are using it as a string. Get rid of '
around this
and you should be fine!
$('div').click(function(){
$(this).append('how r u');
});
Note
If you want to replace the text, you should use .html()
or .text()
instead of .append()
. Append will leave all text in the div as it is, and add the new text at the end.
Upvotes: 5
Reputation: 218762
If you want to replace the text, use the html
method
$('div').click(function(){
$(this).html('how r u');
})
})
you dont need'
with this
.
working sample : http://jsfiddle.net/4xj72/1/
you can use text()
method as well if there is no HTML markups you are adding.
Upvotes: 2
Reputation: 388316
$('this').append('how r u');
should $(this).append('how r u');
. this
is a keyword there should not be '
quotes around it.
Upvotes: 1
Reputation: 1717
<script>
$(document).ready(function(){
//alert('hi');
$('div').append('hi')
$('div').click(function(){
$(this).append('how r u');
})
})
</script>
Upvotes: 1