0

I want to hide text "Demo" in below code using jQuery on page load

<label>
     <span class="demo_logo"></span>
     <input type="radio" name="paymentmethod" > 
     Demo
 </label>
4
  • 1
    You can't without hiding the input... but you can wrap the text in another element and then hide it... or you can remove the text node itself Commented Feb 26, 2015 at 12:05
  • $('label').contents().last().remove() Commented Feb 26, 2015 at 12:07
  • Here is an answer: stackoverflow.com/questions/15196630/… Commented Feb 26, 2015 at 12:09
  • jsfiddle.net/72ag3tc9 Commented Feb 26, 2015 at 12:12

3 Answers 3

1

You can't hide the text node, but you can remove it like

$('label').contents().last().remove();

If you have to do it for more than 1 element

$('label').each(function(){
    if(this.lastChild.nodeType == 3){
        this.removeChild(this.lastChild)
    }
})

If you just want to hide it not remove then wrap it with another element and hide that element

$('label').each(function(){
    if(this.lastChild.nodeType == 3){
        $(this.lastChild).wrap('<span />').parent().hide()
    }
})
Sign up to request clarification or add additional context in comments.

Comments

0

You could change your HTML and do this instead:

<span class='demo_logo'></span>
<input type='radio' id='paymentmethod' name='paymentmethod'>
<label for="paymentmethod">Demo</label>

Then use jQuery and do something like:

$('label[for=paymentmethod]').hide();

Comments

0

Finds all label containing "nabin" and hide them. Finds all label containing "john" and underlines them.

$("label:contains('nabin')").css("display", "none");
$("label:contains('john')").css("text-decoration", "underline");

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Hide label</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
  <label>nabin</label>
  <br>
  <label>john</label>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.