Reputation: 9787
I have a contenteditable div and I want its initial or default value.
In an input it would be $(this).prop('defaultValue')
In a div, I tried:
def = $(this).text()
This gets the initial value but it changes when the user changes the content of the div. Is there a way to get and store that initial value?
Here to check and play: http://jsfiddle.net/appqhxpc/
HTML:
<div id="name" contenteditable="true">name</div>
<div id="check">check</div>
JQUERY:
$("div").focus(function () {
def = $(this).text()
$('#check').text(def);
});
Upvotes: 0
Views: 4379
Reputation: 6894
You can use .attr()
to get the id, which is 'name'
$("div").focus(function () {
def = $(this).attr('id')
$('#check').text(def);
});
Upvotes: 2
Reputation: 2258
check if Def is empty
var def = '';
$("div").focus(function() {
if (def == "") {
def = $(this).text()
$('#check').text(def);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="name" contenteditable="true">name</div>
<div id="check">check</div>
Upvotes: 1
Reputation: 36703
var _val; // global
$(function(){
_val = $("#name").val();
});
Now _val
will always have the initial value, use it whereever you want using window._val
Upvotes: 1