Reputation: 7969
I stored value in .data method. It is not working on first click but working on second click.
<script type="text/javascript">
$(function(){
$('a').click(function(){
var value= $('div').data('key');
var txt= $(this).text();
$('div').data('key',txt)
$('span').text(value)
})})
</script>
// html
<div></div>
<a href="#">My text</a>
<span></span>
Upvotes: 0
Views: 123
Reputation: 9764
This should work - DEMO
On the first click you are tying to access the data before setting it.
<script type="text/javascript">
$(function(){
$('a').click(function(){
var txt= $(this).text();
$('div').data('key',txt)
$('span').text($('div').data('key'))
})
})
</script>
Html
<div></div>
<a href="#">My text</a>
<span></span>
Upvotes: 1
Reputation: 9368
You are accessing the data value before actually setting it
$(function(){
$('a').click(function(){
var value= $('div').data('key'); // accessing data
var txt= $(this).text();
$('div').data('key',txt) // setting data
$('span').text(value)
})
})
Upvotes: 0
Reputation: 8201
You are retrieving it before you are storing it. Try moving var value=...
to below $('div').data(...
Upvotes: 0