html - Jquery show number within div in alert -
hi
i want show number in div in alert this
<script> var sec = $('#demo').html(); alert(sec); </script> <div id="demo">100</div>
code show 100 number
but null or undefined or 0 when use
size(); , text(); , html(); , length;
edit : got problem . jquery v1.7.1 , seems has problem . upgraded jquery v1.9.1 , code works fine . thank answers
set
this techniques set (place) number inside empty <div id="demo"></div>
$('#demo').text(100); // overwrite new content $('#demo').html(100); // overwrites + can insert raw (string) html. $('#demo').append(100); // keeps content , appends element $('#demo').prepend(100); // keeps content , prepends element $('#demo')[0].textcontent = "100"; // jq/js, overwrite text content $('#demo')[0].innerhtml = "100"; // jq/js, overwrite html document.getelementbyid('demo').innerhtml = 100 ; // pure js example
http://jsbin.com/eyuhay/10/edit
get
this techniques get desired content (100) out of <div id="demo">100</div>
var number = $('#demo').text(); // jq var number = $('#demo').html(); // jq var number = $('#demo')[0].innerhtml; // jq/js var number = $('#demo').get(0).innerhtml; // jq/js using .get(0) instead of [0] var number = document.getelementbyid('demo').innerhtml; // pure js var number = document.getelementbyid('demo').textcontent; // pure js
http://jsbin.com/eyuhay/11/edit
.size(), length
.size()
jquery method retrieve number of elements in our object array.
.length
native js method the... length :-d
additionally take @ : http://jsbin.com/eyuhay/6/edit :
alert( number.length ); // 3, cause 100 has 3 digits alert( $('#demo').size() ); // 1, use size() on elements alert( $('#demo').length ); // 1, cause 1 element #id
- wrap code
document ready function
jquery(document).ready(function( $ ) { // dom read , ready manipulated /* code here */ } );
- or place
<script></script>
tags before</body>
Comments
Post a Comment