Ananth
Ananth

Reputation: 110

How to change the text of a <label> which is hidden

I want to change the text of a label whose parent has its display hidden. I am not able to access the label because of its parents visibility.

I would like to know is there a way to change the text of this label without the changing the visibility of the parent.

I tried changing the visibility of the label and changing the text and hiding it back, did not work.

$(document).ready(function(){
$("#394").find(".name").children("label").text("Home");

});

<div class="sample1" style="display: none;">
<div id="394">
    <div class="name">
        <label>house.jpg</label>
    </div>
</div>
</div>

Any suggestions/ideas?

Answer:

First, Thank you all for the response.

I fixed the issue.

This is the change I made and it worked.

$(document).ready(function(){
$(".sample1").find(".name").children("label").text("Home");
});

<div class="sample1" style="display: block;">
<div style="display: none;" id="398">
    <div class="name">
        <label>Sample</label>
    </div>
</div>
</div>

Upvotes: 1

Views: 2544

Answers (3)

Smamatti
Smamatti

Reputation: 3931

Sample
(Updated) http://jsfiddle.net/SXLnt/1/
(Old) http://jsfiddle.net/SXLnt/

Solution

$("#394 label").text("new_house.jpg");
$(".sample1").show(); // show hidden result

Sample HTML

<form id="parent_form" style="display: none; visibility: hidden;">
  <label for="male" id="lb_male">Male</label>
  <input type="radio" name="sex" id="male" />
  <br />
  <label for="female" id="lb_female">Female</label>
  <input type="radio" name="sex" id="female" />
</form> 

Sample JS (jQuery)

$("#lb_male").text("Man");     // Change text 1
$("#lb_female").text("Woman"); // Change text 2
//$("#parent_form").show();      // Show parent

Sample DOM result
JSFiddle result

Upvotes: 4

InvisibleBacon
InvisibleBacon

Reputation: 3222

If what you posted is your full code, it looks like you are missing a closing tag on your outer div. Try this:

<div class="sample1" style="display: none;">
  <div id="394">
    <div class="name">
        <label>house.jpg</label>
    </div>
  </div>
</div>

Upvotes: 0

Obi
Obi

Reputation: 49

IF the label's ID is lblSomething, you could do it in JQuery as so ... $("lblSomething").val("Changed value") Hope it helps.

Upvotes: 0

Related Questions