This is basic.
How do I get the value \'This is my name\' of the above span?
This is my name
$('span id').text(); worked with me
You could use id in span directly in your html.
<span id="span_id">Client</span>
Then your jQuery code would be
$("#span_id").text();
Some one helped me to check errors and found that he used val() instead of text(), it is not possible to use val() function in span. So
$("#span_id").val();
will return null.
Since you did not provide an attribute for the 'item' value, I am assuming a class is being used:
<div class='item1'>
<span>This is my name</span>
</div>
alert($(".item span").text());
Make sure you wait for the DOM to load to use your code, in jQuery you use the ready()
function for that:
<html>
<head>
<title>jQuery test</title>
<!-- script that inserts jquery goes here -->
<script type='text/javascript'>
$(document).ready(function() { alert($(".item span").text()); });
</script>
</head>
<body>
<div class='item1'>
<span>This is my name</span>
</div>
</body>
$('#item1 span').html();
Its working with my code
Assuming you intended it to read id="item1", you need
$('#item1 span').text()
In javascript wouldn't you use document.getElementById('item1').innertext
?