2

I have the following markup:

<span>
Some text blalablalalbal<br/><br/>
"You are Awesome" <br/><br/>
----What can I do for you?<br/><br/>
</span>

Now I want to hide first line and modify the last line.

How can I grab those text using jQuery?

Note:

1: There are multiple instances of similar code-block but with different text. So I won't be able to hardcode. I am wondering if I can split it using
tags somehow?

2: If it can be done in server-side code in C#, that is fine as well.

4 Answers 4

3

You can play around with textnodes doing something like this.


Update

var $nodes = $('span').contents().map(function(a,b){
    return (b.nodeType===3?b:null);
});

// hide first line
$($nodes.get(0)).wrap('<span style="display:none;" />');
$nodes.get(2).data = "foo";

Working example of what I mean.

2
0

CSS has :first-line and :last-line modifiers.

Try:

span:first-line {
  display: none;
}

Use jQuery with selector to modify last line:

var content = $('span:last-line').html();
content += " - Modified";
$('span:last-line').html(content);
2
  • I haven't tested the above code, but it should work in theory! :D Oct 6, 2011 at 19:36
  • I don’t think jQuery has a :first-line or :last-line selector. Oct 6, 2011 at 19:48
0

You can split the line breaks and modify the array:

var elm = $('span'),
    lines = elm.html().split('<br>');

lines.shift(); // removes the first
lines[lines.length-3] += '-modified'; // modify the last

elm.html(lines.join('<br>'));
0

Or you can put the three lines in three different spans with ids. Something like:

<span>
<span id="sentence1">Some text blalablalalbal</span><br/><br/>
<span id="sentence2">"You are Awesome" </span><br/><br/>
<span id="sentence3">----What can I do for you?</span><br/><br/>
</span>

And hiding the first sentence with jquery:

$("span#sentence1").hide();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.