0

I have an html element like this:

<dd><span class="label label-success" id="status">Production</span></dd>

I want to change it to

<dd><span class="label label-warning" id="status">Idle</span>

based on a ws.socket message.

setTimeout(function(){
            window.ws = new WebSocket('ws://localhost:8088/sock/123');
            window.ws.onmessage = function (evt) {
                field.value = "Idle"
            };
            window.ws.onclose = function (evt){

            }
            window.ws.onopen = function (evt){

            }

        }, 500);

I want to change the value from Production to Idle and the class to "label label-warning". I know to change the value I would do this:

var field = document.getElementById("status")

but I'm not exactly sure how to change the class and would using field.value be the correct way to change a span text?

user2769651
  • 171
  • 1
  • 3
  • 15

4 Answers4

2

You have jQuery, use it!

$("#status").text("Idle").removeClass("label-success").addClass("label-warning");

Or just .toggleClass("label-success label-warning");

tymeJV
  • 103,943
  • 14
  • 161
  • 157
1

Use Jquery

Try this

$('#status').text('Your Text')  //For setting text
$('#status').removeClass('label-success').addClass('label-warning')
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
1

If you are just wanting to use JavaScript and not jQuery, you can refer to this answer. You can easily set the class by just saying:

document.getElementById("whatever").className = "";
Community
  • 1
  • 1
0

To change the class (this is much more convenient using jQuery):

document.getElementsById('status').setAttribute("class","label label-warning");

To change the value of the span text:

document.getElementsById('status').innerHTML = "Production";
Web User
  • 7,438
  • 14
  • 64
  • 92