getting the first line of text in an element jquery
up vote
9
down vote
favorite
I have an example element like this:
<div id="element">
Blah blah blah.
<div>Header</div>
...
</div>
it can also look like this:
<div id="element">
<span>Blah blah blah.</span>
<h4>Header</h4>
...
</div>
I want to get the first line (defining line loosely) (Blah blah blah.) of text inside the element. It might be wrapped inside a child element or just be naked textnode. How do I do that? Thanks.
javascript jquery html
add a comment |
up vote
9
down vote
favorite
I have an example element like this:
<div id="element">
Blah blah blah.
<div>Header</div>
...
</div>
it can also look like this:
<div id="element">
<span>Blah blah blah.</span>
<h4>Header</h4>
...
</div>
I want to get the first line (defining line loosely) (Blah blah blah.) of text inside the element. It might be wrapped inside a child element or just be naked textnode. How do I do that? Thanks.
javascript jquery html
add a comment |
up vote
9
down vote
favorite
up vote
9
down vote
favorite
I have an example element like this:
<div id="element">
Blah blah blah.
<div>Header</div>
...
</div>
it can also look like this:
<div id="element">
<span>Blah blah blah.</span>
<h4>Header</h4>
...
</div>
I want to get the first line (defining line loosely) (Blah blah blah.) of text inside the element. It might be wrapped inside a child element or just be naked textnode. How do I do that? Thanks.
javascript jquery html
I have an example element like this:
<div id="element">
Blah blah blah.
<div>Header</div>
...
</div>
it can also look like this:
<div id="element">
<span>Blah blah blah.</span>
<h4>Header</h4>
...
</div>
I want to get the first line (defining line loosely) (Blah blah blah.) of text inside the element. It might be wrapped inside a child element or just be naked textnode. How do I do that? Thanks.
javascript jquery html
javascript jquery html
edited Sep 11 '11 at 20:34
asked Sep 11 '11 at 20:27
Harry
17.1k53139223
17.1k53139223
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
up vote
16
down vote
accepted
Use the contents()[docs] method to get all children, including text nodes, filter out any empty (or whitespace only) nodes, then grab the first of the set.
var first_line = $("#element")
.contents()
.filter(function() {
return !!$.trim( this.innerHTML || this.data );
})
.first();
text node: http://jsfiddle.net/Yftnh/
element: http://jsfiddle.net/Yftnh/1/
1
Hi can you explain why you do return !!? What is !!?
– Harry
Sep 11 '11 at 20:53
1
It converts to the value's boolean equivalent. After trimming, if.innerHTMLor.datareturns an empty string,!!will convert it to booleanfalse, because an empty string is considered a "falsey" value. Any non-empty string will result intrue. stackoverflow.com/questions/4686583/…
– user113716
Sep 11 '11 at 20:54
2
+1 from me, but the performance-wary might want to consider using$.each()rather thanfilter(). It's a bit late in the day for me, but an example of this is jsfiddle.net/AndyE/Yftnh/3 (ignore revision 2, I'm half asleep here!)
– Andy E
Sep 11 '11 at 20:56
2
@Andy E: Nice one. Great use of the comma operator too. @Harry: It just lets you stop the loop once you've found a non-empty node. With.filter()you have to let the loop run for each element that.contents()returns.
– user113716
Sep 11 '11 at 20:58
1
@Andy E: For fun, here's one sans commareturn !$.trim((first_line = this).innerHTML||this.data);:)
– user113716
Sep 11 '11 at 21:03
|
show 6 more comments
up vote
2
down vote
Here is idea for you. Of course if there is h4 element before line you want to get:
var content = $('#element').html();
var arr = content.split('<h4>');
console.log(arr[0]);
no h4 guaranteed, it's just an example
– Harry
Sep 11 '11 at 20:36
add a comment |
up vote
2
down vote
var myElement = $("#element");
while(myElement.children().length > 0)
{
myElement = myElement.children().first();
}
var firstText = myElement.text();
Assuming that the text is correctly wrapped inside an element, of course. You might check whether there's text before the first element:
/s*</.test(myElement.html())
Doesn't work for the first case I believe, that'll match the Header rather than the blah blah blah (maybe should've picked better example text lol)
– Harry
Sep 11 '11 at 20:36
If you get <div id="element"><span></span>mytext</div> it gets pretty difficult. You need something recursively traveling through the DOM i guess. How bulletproof does it have to be?
– Maximilian Hils
Sep 11 '11 at 20:42
add a comment |
up vote
0
down vote
first get the content of the element
var content = $('#element').html();
then split the text into an array using line break
tmp = content.split("n");
the first part of the array is the first line of the element
var firstLine = tmp[0];
1
I first thought, what linebreak, but I see in Fx at least there is. It is however dangerous to rely on. Here is the first line:alert($("#element").text().split(/n/)[1])the [0]th is an empty string... jsfiddle.net/mplungjan/QquSp
– mplungjan
Sep 11 '11 at 20:33
there's no linebreak
– Harry
Sep 11 '11 at 20:33
add a comment |
up vote
0
down vote
the counter starts at 1
myElement.text().split('n')[1]
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
16
down vote
accepted
Use the contents()[docs] method to get all children, including text nodes, filter out any empty (or whitespace only) nodes, then grab the first of the set.
var first_line = $("#element")
.contents()
.filter(function() {
return !!$.trim( this.innerHTML || this.data );
})
.first();
text node: http://jsfiddle.net/Yftnh/
element: http://jsfiddle.net/Yftnh/1/
1
Hi can you explain why you do return !!? What is !!?
– Harry
Sep 11 '11 at 20:53
1
It converts to the value's boolean equivalent. After trimming, if.innerHTMLor.datareturns an empty string,!!will convert it to booleanfalse, because an empty string is considered a "falsey" value. Any non-empty string will result intrue. stackoverflow.com/questions/4686583/…
– user113716
Sep 11 '11 at 20:54
2
+1 from me, but the performance-wary might want to consider using$.each()rather thanfilter(). It's a bit late in the day for me, but an example of this is jsfiddle.net/AndyE/Yftnh/3 (ignore revision 2, I'm half asleep here!)
– Andy E
Sep 11 '11 at 20:56
2
@Andy E: Nice one. Great use of the comma operator too. @Harry: It just lets you stop the loop once you've found a non-empty node. With.filter()you have to let the loop run for each element that.contents()returns.
– user113716
Sep 11 '11 at 20:58
1
@Andy E: For fun, here's one sans commareturn !$.trim((first_line = this).innerHTML||this.data);:)
– user113716
Sep 11 '11 at 21:03
|
show 6 more comments
up vote
16
down vote
accepted
Use the contents()[docs] method to get all children, including text nodes, filter out any empty (or whitespace only) nodes, then grab the first of the set.
var first_line = $("#element")
.contents()
.filter(function() {
return !!$.trim( this.innerHTML || this.data );
})
.first();
text node: http://jsfiddle.net/Yftnh/
element: http://jsfiddle.net/Yftnh/1/
1
Hi can you explain why you do return !!? What is !!?
– Harry
Sep 11 '11 at 20:53
1
It converts to the value's boolean equivalent. After trimming, if.innerHTMLor.datareturns an empty string,!!will convert it to booleanfalse, because an empty string is considered a "falsey" value. Any non-empty string will result intrue. stackoverflow.com/questions/4686583/…
– user113716
Sep 11 '11 at 20:54
2
+1 from me, but the performance-wary might want to consider using$.each()rather thanfilter(). It's a bit late in the day for me, but an example of this is jsfiddle.net/AndyE/Yftnh/3 (ignore revision 2, I'm half asleep here!)
– Andy E
Sep 11 '11 at 20:56
2
@Andy E: Nice one. Great use of the comma operator too. @Harry: It just lets you stop the loop once you've found a non-empty node. With.filter()you have to let the loop run for each element that.contents()returns.
– user113716
Sep 11 '11 at 20:58
1
@Andy E: For fun, here's one sans commareturn !$.trim((first_line = this).innerHTML||this.data);:)
– user113716
Sep 11 '11 at 21:03
|
show 6 more comments
up vote
16
down vote
accepted
up vote
16
down vote
accepted
Use the contents()[docs] method to get all children, including text nodes, filter out any empty (or whitespace only) nodes, then grab the first of the set.
var first_line = $("#element")
.contents()
.filter(function() {
return !!$.trim( this.innerHTML || this.data );
})
.first();
text node: http://jsfiddle.net/Yftnh/
element: http://jsfiddle.net/Yftnh/1/
Use the contents()[docs] method to get all children, including text nodes, filter out any empty (or whitespace only) nodes, then grab the first of the set.
var first_line = $("#element")
.contents()
.filter(function() {
return !!$.trim( this.innerHTML || this.data );
})
.first();
text node: http://jsfiddle.net/Yftnh/
element: http://jsfiddle.net/Yftnh/1/
answered Sep 11 '11 at 20:41
user113716
257k55399416
257k55399416
1
Hi can you explain why you do return !!? What is !!?
– Harry
Sep 11 '11 at 20:53
1
It converts to the value's boolean equivalent. After trimming, if.innerHTMLor.datareturns an empty string,!!will convert it to booleanfalse, because an empty string is considered a "falsey" value. Any non-empty string will result intrue. stackoverflow.com/questions/4686583/…
– user113716
Sep 11 '11 at 20:54
2
+1 from me, but the performance-wary might want to consider using$.each()rather thanfilter(). It's a bit late in the day for me, but an example of this is jsfiddle.net/AndyE/Yftnh/3 (ignore revision 2, I'm half asleep here!)
– Andy E
Sep 11 '11 at 20:56
2
@Andy E: Nice one. Great use of the comma operator too. @Harry: It just lets you stop the loop once you've found a non-empty node. With.filter()you have to let the loop run for each element that.contents()returns.
– user113716
Sep 11 '11 at 20:58
1
@Andy E: For fun, here's one sans commareturn !$.trim((first_line = this).innerHTML||this.data);:)
– user113716
Sep 11 '11 at 21:03
|
show 6 more comments
1
Hi can you explain why you do return !!? What is !!?
– Harry
Sep 11 '11 at 20:53
1
It converts to the value's boolean equivalent. After trimming, if.innerHTMLor.datareturns an empty string,!!will convert it to booleanfalse, because an empty string is considered a "falsey" value. Any non-empty string will result intrue. stackoverflow.com/questions/4686583/…
– user113716
Sep 11 '11 at 20:54
2
+1 from me, but the performance-wary might want to consider using$.each()rather thanfilter(). It's a bit late in the day for me, but an example of this is jsfiddle.net/AndyE/Yftnh/3 (ignore revision 2, I'm half asleep here!)
– Andy E
Sep 11 '11 at 20:56
2
@Andy E: Nice one. Great use of the comma operator too. @Harry: It just lets you stop the loop once you've found a non-empty node. With.filter()you have to let the loop run for each element that.contents()returns.
– user113716
Sep 11 '11 at 20:58
1
@Andy E: For fun, here's one sans commareturn !$.trim((first_line = this).innerHTML||this.data);:)
– user113716
Sep 11 '11 at 21:03
1
1
Hi can you explain why you do return !!? What is !!?
– Harry
Sep 11 '11 at 20:53
Hi can you explain why you do return !!? What is !!?
– Harry
Sep 11 '11 at 20:53
1
1
It converts to the value's boolean equivalent. After trimming, if
.innerHTML or .data returns an empty string, !! will convert it to boolean false, because an empty string is considered a "falsey" value. Any non-empty string will result in true. stackoverflow.com/questions/4686583/…– user113716
Sep 11 '11 at 20:54
It converts to the value's boolean equivalent. After trimming, if
.innerHTML or .data returns an empty string, !! will convert it to boolean false, because an empty string is considered a "falsey" value. Any non-empty string will result in true. stackoverflow.com/questions/4686583/…– user113716
Sep 11 '11 at 20:54
2
2
+1 from me, but the performance-wary might want to consider using
$.each() rather than filter(). It's a bit late in the day for me, but an example of this is jsfiddle.net/AndyE/Yftnh/3 (ignore revision 2, I'm half asleep here!)– Andy E
Sep 11 '11 at 20:56
+1 from me, but the performance-wary might want to consider using
$.each() rather than filter(). It's a bit late in the day for me, but an example of this is jsfiddle.net/AndyE/Yftnh/3 (ignore revision 2, I'm half asleep here!)– Andy E
Sep 11 '11 at 20:56
2
2
@Andy E: Nice one. Great use of the comma operator too. @Harry: It just lets you stop the loop once you've found a non-empty node. With
.filter() you have to let the loop run for each element that .contents() returns.– user113716
Sep 11 '11 at 20:58
@Andy E: Nice one. Great use of the comma operator too. @Harry: It just lets you stop the loop once you've found a non-empty node. With
.filter() you have to let the loop run for each element that .contents() returns.– user113716
Sep 11 '11 at 20:58
1
1
@Andy E: For fun, here's one sans comma
return !$.trim((first_line = this).innerHTML||this.data); :)– user113716
Sep 11 '11 at 21:03
@Andy E: For fun, here's one sans comma
return !$.trim((first_line = this).innerHTML||this.data); :)– user113716
Sep 11 '11 at 21:03
|
show 6 more comments
up vote
2
down vote
Here is idea for you. Of course if there is h4 element before line you want to get:
var content = $('#element').html();
var arr = content.split('<h4>');
console.log(arr[0]);
no h4 guaranteed, it's just an example
– Harry
Sep 11 '11 at 20:36
add a comment |
up vote
2
down vote
Here is idea for you. Of course if there is h4 element before line you want to get:
var content = $('#element').html();
var arr = content.split('<h4>');
console.log(arr[0]);
no h4 guaranteed, it's just an example
– Harry
Sep 11 '11 at 20:36
add a comment |
up vote
2
down vote
up vote
2
down vote
Here is idea for you. Of course if there is h4 element before line you want to get:
var content = $('#element').html();
var arr = content.split('<h4>');
console.log(arr[0]);
Here is idea for you. Of course if there is h4 element before line you want to get:
var content = $('#element').html();
var arr = content.split('<h4>');
console.log(arr[0]);
answered Sep 11 '11 at 20:33
simoncereska
2,6491223
2,6491223
no h4 guaranteed, it's just an example
– Harry
Sep 11 '11 at 20:36
add a comment |
no h4 guaranteed, it's just an example
– Harry
Sep 11 '11 at 20:36
no h4 guaranteed, it's just an example
– Harry
Sep 11 '11 at 20:36
no h4 guaranteed, it's just an example
– Harry
Sep 11 '11 at 20:36
add a comment |
up vote
2
down vote
var myElement = $("#element");
while(myElement.children().length > 0)
{
myElement = myElement.children().first();
}
var firstText = myElement.text();
Assuming that the text is correctly wrapped inside an element, of course. You might check whether there's text before the first element:
/s*</.test(myElement.html())
Doesn't work for the first case I believe, that'll match the Header rather than the blah blah blah (maybe should've picked better example text lol)
– Harry
Sep 11 '11 at 20:36
If you get <div id="element"><span></span>mytext</div> it gets pretty difficult. You need something recursively traveling through the DOM i guess. How bulletproof does it have to be?
– Maximilian Hils
Sep 11 '11 at 20:42
add a comment |
up vote
2
down vote
var myElement = $("#element");
while(myElement.children().length > 0)
{
myElement = myElement.children().first();
}
var firstText = myElement.text();
Assuming that the text is correctly wrapped inside an element, of course. You might check whether there's text before the first element:
/s*</.test(myElement.html())
Doesn't work for the first case I believe, that'll match the Header rather than the blah blah blah (maybe should've picked better example text lol)
– Harry
Sep 11 '11 at 20:36
If you get <div id="element"><span></span>mytext</div> it gets pretty difficult. You need something recursively traveling through the DOM i guess. How bulletproof does it have to be?
– Maximilian Hils
Sep 11 '11 at 20:42
add a comment |
up vote
2
down vote
up vote
2
down vote
var myElement = $("#element");
while(myElement.children().length > 0)
{
myElement = myElement.children().first();
}
var firstText = myElement.text();
Assuming that the text is correctly wrapped inside an element, of course. You might check whether there's text before the first element:
/s*</.test(myElement.html())
var myElement = $("#element");
while(myElement.children().length > 0)
{
myElement = myElement.children().first();
}
var firstText = myElement.text();
Assuming that the text is correctly wrapped inside an element, of course. You might check whether there's text before the first element:
/s*</.test(myElement.html())
edited Sep 11 '11 at 20:40
answered Sep 11 '11 at 20:34
Maximilian Hils
3,80421331
3,80421331
Doesn't work for the first case I believe, that'll match the Header rather than the blah blah blah (maybe should've picked better example text lol)
– Harry
Sep 11 '11 at 20:36
If you get <div id="element"><span></span>mytext</div> it gets pretty difficult. You need something recursively traveling through the DOM i guess. How bulletproof does it have to be?
– Maximilian Hils
Sep 11 '11 at 20:42
add a comment |
Doesn't work for the first case I believe, that'll match the Header rather than the blah blah blah (maybe should've picked better example text lol)
– Harry
Sep 11 '11 at 20:36
If you get <div id="element"><span></span>mytext</div> it gets pretty difficult. You need something recursively traveling through the DOM i guess. How bulletproof does it have to be?
– Maximilian Hils
Sep 11 '11 at 20:42
Doesn't work for the first case I believe, that'll match the Header rather than the blah blah blah (maybe should've picked better example text lol)
– Harry
Sep 11 '11 at 20:36
Doesn't work for the first case I believe, that'll match the Header rather than the blah blah blah (maybe should've picked better example text lol)
– Harry
Sep 11 '11 at 20:36
If you get <div id="element"><span></span>mytext</div> it gets pretty difficult. You need something recursively traveling through the DOM i guess. How bulletproof does it have to be?
– Maximilian Hils
Sep 11 '11 at 20:42
If you get <div id="element"><span></span>mytext</div> it gets pretty difficult. You need something recursively traveling through the DOM i guess. How bulletproof does it have to be?
– Maximilian Hils
Sep 11 '11 at 20:42
add a comment |
up vote
0
down vote
first get the content of the element
var content = $('#element').html();
then split the text into an array using line break
tmp = content.split("n");
the first part of the array is the first line of the element
var firstLine = tmp[0];
1
I first thought, what linebreak, but I see in Fx at least there is. It is however dangerous to rely on. Here is the first line:alert($("#element").text().split(/n/)[1])the [0]th is an empty string... jsfiddle.net/mplungjan/QquSp
– mplungjan
Sep 11 '11 at 20:33
there's no linebreak
– Harry
Sep 11 '11 at 20:33
add a comment |
up vote
0
down vote
first get the content of the element
var content = $('#element').html();
then split the text into an array using line break
tmp = content.split("n");
the first part of the array is the first line of the element
var firstLine = tmp[0];
1
I first thought, what linebreak, but I see in Fx at least there is. It is however dangerous to rely on. Here is the first line:alert($("#element").text().split(/n/)[1])the [0]th is an empty string... jsfiddle.net/mplungjan/QquSp
– mplungjan
Sep 11 '11 at 20:33
there's no linebreak
– Harry
Sep 11 '11 at 20:33
add a comment |
up vote
0
down vote
up vote
0
down vote
first get the content of the element
var content = $('#element').html();
then split the text into an array using line break
tmp = content.split("n");
the first part of the array is the first line of the element
var firstLine = tmp[0];
first get the content of the element
var content = $('#element').html();
then split the text into an array using line break
tmp = content.split("n");
the first part of the array is the first line of the element
var firstLine = tmp[0];
answered Sep 11 '11 at 20:33
clem
2,42211632
2,42211632
1
I first thought, what linebreak, but I see in Fx at least there is. It is however dangerous to rely on. Here is the first line:alert($("#element").text().split(/n/)[1])the [0]th is an empty string... jsfiddle.net/mplungjan/QquSp
– mplungjan
Sep 11 '11 at 20:33
there's no linebreak
– Harry
Sep 11 '11 at 20:33
add a comment |
1
I first thought, what linebreak, but I see in Fx at least there is. It is however dangerous to rely on. Here is the first line:alert($("#element").text().split(/n/)[1])the [0]th is an empty string... jsfiddle.net/mplungjan/QquSp
– mplungjan
Sep 11 '11 at 20:33
there's no linebreak
– Harry
Sep 11 '11 at 20:33
1
1
I first thought, what linebreak, but I see in Fx at least there is. It is however dangerous to rely on. Here is the first line:
alert($("#element").text().split(/n/)[1]) the [0]th is an empty string... jsfiddle.net/mplungjan/QquSp– mplungjan
Sep 11 '11 at 20:33
I first thought, what linebreak, but I see in Fx at least there is. It is however dangerous to rely on. Here is the first line:
alert($("#element").text().split(/n/)[1]) the [0]th is an empty string... jsfiddle.net/mplungjan/QquSp– mplungjan
Sep 11 '11 at 20:33
there's no linebreak
– Harry
Sep 11 '11 at 20:33
there's no linebreak
– Harry
Sep 11 '11 at 20:33
add a comment |
up vote
0
down vote
the counter starts at 1
myElement.text().split('n')[1]
add a comment |
up vote
0
down vote
the counter starts at 1
myElement.text().split('n')[1]
add a comment |
up vote
0
down vote
up vote
0
down vote
the counter starts at 1
myElement.text().split('n')[1]
the counter starts at 1
myElement.text().split('n')[1]
answered Nov 8 at 8:46
Mo D Genesis
14010
14010
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7381012%2fgetting-the-first-line-of-text-in-an-element-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown