In this post we will see set a value to span tag using JQuer. To change value of span we can access span tag directly like $('span') and then using its .html() method we can assign some text to it.
Example to assign text to span [jQuery]
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<title>Assign value to span in jQuery</title>
<script type="text/javascript">
$(document).ready(function () {
$("#submitbtn").click(function () {
var inputValue = $("#inputText").val();
$('span').html(inputValue);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label>Type some text here to assign to span</label>
<input type="text" id="inputText"/>
<input type="button" id="submitbtn" value="Submit" /><br/>
<span style="width:500px;height:120px; background-color:thistle"> </span>
</div>
</form>
</body>
</html>
Summary:So to replace span tag text we get the object of span tag like $('span') and using .html() we can change value of span.