1
<input type="checkbox" name="Rates" class="RatesClass">Rates & Fees<br />

How can I change that text Rates & Fees dynamically, using Jquery?

Thanks!

How about this one that doesn't have a class defined?

<input type="checkbox" id="Value" name="Values">Values<br />

EDIT:

<input type="checkbox" id="RatesFees" name="Rates" class="RatesClass">Rates & Fees<br />
<input type="checkbox" id="RatesOnly" name="RatesSpecific" class="RatesClass">Rates (All-In-Rate Only)<br />
6
  • From where? You don't need Jquery to change the text, using DOM functions: yourInput.NextSibling.Data='new text'; Commented Feb 28, 2011 at 15:48
  • 1
    Generally you would put a <label> around "Rates & Fees" and set its for attribute to "Rates" so that clicking on the text will also mark the checkbox. That would also give you the label tag to select on. Commented Feb 28, 2011 at 15:48
  • @Martin The text isn't a child of the input. It's a sibling and it doesn't have any tags around it so it wold be enclosed in the parent domnode of the input. Commented Feb 28, 2011 at 15:48
  • Generally you would also close the input tag at the end using /> Commented Feb 28, 2011 at 15:49
  • @Endophage Just saw and changed that, I mistook the <br /> for an end tag... Commented Feb 28, 2011 at 15:49

2 Answers 2

2

The simplest will be to unwrap the input DOM element from the jQuery object, and use the native nextSibling property to get the text node, then update its value with data.

$('input.RatesClass')[0].nextSibling.data = "new value";

If you're in an event handler, you'd just use this to refer to the element.

$('input.RatesClass').change(function() {
    this.nextSibling.data = "new value";
});
Sign up to request clarification or add additional context in comments.

9 Comments

What about doing it to an element that does not have a class defined, but everything else is defined the same way?
@slandau: You need some way to select the input. The best way to select it will depend on the actual situation. If you're doing this in an event handler where the handler is on the <input>, then just use the this keyword, which will refer to the element. I'll update.
@patrick, check my edit. That input has no class defined and I cannot use the Id with the way you specified, nor the Jquery .text() property.
@patrick dw The text isn't inside its own domnode so it isn't a sibling of the <input>. nextSibling would return the <br />
So there's no way to get at that?
|
-1

you need to add an id to the input.

<input id="myInput" type="checkbox" name="Rates" class="RatesClass">Rates & Fees<br/>

Then in your jQuery.

$('#myInput').html('Here is some HTML')

1 Comment

Where did you find innerHTML here?

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.