I am using JQuery for my application. In my code, I want to get only the text 'Firstname' in the label.
I tried it with
$("#label"+div_id+"").html(); //but displays Firstname along with the span tag..
But I only need Firstname. How can i do so?
The following is my Html code
<label id="label1">Firstname<span class="req"><em> * </em></span></label>
The general solution to this problem -- namely, getting immediate text but not child text -- is elem.clone().children().remove().end().text();
(where elem here is $("#label"+div_id)
). This has the same result as karim79's solution but with the benefit of not breaking if other tags are added to the label.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With