Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve label content in Jquery

Tags:

jquery

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>
like image 837
useranon Avatar asked Dec 23 '22 10:12

useranon


1 Answers

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.

like image 113
ozan Avatar answered Dec 24 '22 23:12

ozan