Jquery to update a label

ambuli
ambuli used Ask the Experts™
on
Hi there,
I have the following JS.

        var expandCollapseLabel = document.createElement("label");
        expandCollapseLabel.id = "ExpandCollapseLabelID";
        expandCollapseLabel.innerHTML = "-";

Later on, in another function I have to update the label.
I tried the following.  It is getting called, but the label is not updated.  How can I do this?  Thanks.

 $("#ExpandCollapseLabelID").val("+");
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Like this
expandCollapseLabel.innerHTML = "+";

Open in new window

Top Expert 2015
Commented:
A label does not have a value property. Use $("#ExpandCollapseLabelID").text("+").
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
.val() is used for input elements not Label's - you have to treat the label as you would any other text element.
You also seem to be mixing JQuery and straight javascript - to update the element using JQuery as you are attempting you can do this

 $("#ExpandCollapseLabelID").html("+"); 

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Here is a complete sample using just JQuery
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<a>Click</a>
<script src="http://code.jquery.com/jquery.js"></script>

<script type="text/javascript">
$(function() {
	var el = $('<label/>').attr('id', 'ExpandCollapseLabelID').html('-').appendTo('body');

	$('a').click(function(e) {
		e.preventDefault();
		el.html("+");
	});
});
</script>
</body>
</html>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial