2
<label>
  Auto zoom 
  <input type="checkbox" class="js-switch" id="autoZoom" data-switchery="true" style="display: none;">
  <span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); background-color: rgb(255, 255, 255); transition: border 0.4s, box-shadow 0.4s;">
    <small style="left: 0px; transition: background-color 0.4s, left 0.2s;"></small>
  </span>
</label>

I have this code above, I want to replace "Auto zoom" to "Test", how do I do that?

$("#div label:contains('Auto zoom')").text("Test");

I tried using the code above, which is obviously not working, it will remove the input type checkbox behind too...

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Alvin
  • 45
  • 7

4 Answers4

1

The easiest way to do this would be to wrap the text in another element, like a span, then change the text().

Assuming you cannot change the HTML, then you can do what you require by using contents() and retrieving the first textNode, like this:

var label = $('label').contents().filter(function() {
  return this.nodeType == 3 && this.nodeValue.trim();
})[0];

label.nodeValue = label.nodeValue.replace('Auto zoom', 'Test');
.switchery-small {
  box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset;
  border-color: rgb(223, 223, 223);
  background-color: rgb(255, 255, 255);
  transition: border 0.4s, box-shadow 0.4s;
}

.switchery-small small {
  left: 0px;
  transition: background-color 0.4s, left 0.2s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
  Auto zoom Foo Bar
  <input type="checkbox" class="js-switch" id="autoZoom" data-switchery="true" style="display: none;">
  <span class="switchery switchery-small">
    <small></small>
  </span>
</label>

Also note that I moved the inline style attribute out to rules in an external stylesheet. This is much better as it de-clutters the HTML and separate the HTML from the styling.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

I would recommend wrapping your text around span tag.

$(".labelTitle").text("Test");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<span class="labelTitle">Auto zoom </span>
<input type="checkbox" class="js-switch" id="autoZoom" data-switchery="true" style="display: none;">
<span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); background-color: rgb(255, 255, 255); transition: border 0.4s, box-shadow 0.4s;">
<small style="left: 0px; transition: background-color 0.4s, left 0.2s;"></small>
</span>
</label>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • I'm not the one coded these, I'm using jquery to edit a web game..
    How should i add span?
    – Alvin May 22 '17 at 09:03
0

Try Code: Just Remove # and Div

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<label>Auto zoom <input type="checkbox" class="js-switch" id="autoZoom" data-switchery="true" style="display: none;"><span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); background-color: rgb(255, 255, 255); transition: border 0.4s, box-shadow 0.4s;"><small style="left: 0px; transition: background-color 0.4s, left 0.2s;"></small></span></label>

<script type="text/javascript">
$(document).ready(function(){
$("label:contains('Auto zoom')").text("Test");
});
</script>
kari kalan
  • 497
  • 3
  • 20
-2

You have to select the with:

$("label:contains('Auto zoom')").text('Test');

With your jquery:

$('#div')...

You are selecting a element with id='div'. If you can put a id or class in the label element, is too much easy, like:

<label id='label_test'> Auto zoom...</label>

Then you can do like:

$('#label_test').text("Test");

Hope it helps!

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39