0

I am having trouble making the class change depending on text that changes automatically.

<div class="WAIT">
    <p id="device_display">WAIT</p>
</div>

The text changes to WAIT, ON, OFF.

NOTE: The text changes automatically using MQTT.

Any help would be appreciated.

Soul Gamer
  • 125
  • 1
  • 2
  • 7

3 Answers3

1

You could create a class called changeText and use that to update the values.

jQuery:

$(".changeText").attr('class', $(".changeText").innerHTML+' changeText');

HTML:

<div class="WAIT changeText">
    <p id="device_display">WAIT</p>
</div>
Ryland Goldman
  • 95
  • 1
  • 14
1

Add a class .parent and try these scripts:

$('#device_display').bind("DOMSubtreeModified", function() {
  $(this).closest('.parent')
    .removeClass('WAIT ON OFF')
    .addClass($(this).text());
});

$('input').on('change', function() {
  $('#device_display').text($(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parent WAIT">
    <p id="device_display">WAIT</p>
</div>

<hr>
<input type="text">

Is this what you are looking for?

Chaska
  • 3,165
  • 1
  • 11
  • 17
1
<div class="WAIT" id="changeid">
   <p id="device_display">WAIT</p>
</div>

var htmltxt = $("#device_display").html();
$("#changeid").attr('class', htmltxt);

Not sure if this is what you looking for, this is the possible answer I can give based on my knowledge.

Links to refer:

J-Query change class name

J-Query .html documentation

LEE Hau Chon
  • 435
  • 3
  • 12