1

Code is as follows:

<h1 class="slide-title">
<span class="slider-category">
<a href="/test/test.html"> Blocking irritating text</a>
</span>
Hong Kong Trip Nov 2015 – Day 3 Itinerary
</h1>

Using h1.slide-title:hover will affect the "Blocking irritating text" within the span element as well.

I ONLY want to underline on hover the following text "Hong Kong Trip Nov 2015 – Day 3 Itinerary", I DO NOT want the "Blocking irritating text" to be underlined when I hover over it.

Is this possible with CSS or jQuery?

Edit: question is not duplicate as I do not want to edit the HTML. Also as a side note, I would like the <span> element to underline on hover separately as well.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
David Z
  • 93
  • 1
  • 3
  • 19

4 Answers4

7

You will have to change the Element you don't want to be affected by text-decoration a little with display: inline-block:

h1.slide-title:hover {
  text-decoration: underline;
}
h1.slide-title a {
  display: inline-block;
  text-decoration: none;
}
h1.slide-title a:hover {
  text-decoration: underline;
}
<h1 class="slide-title">
  <span class="slider-category">
    <a href="/test/test.html">Blocking irritating text</a>
  </span>
  Hong Kong Trip Nov 2015 – Day 3 Itinerary
</h1>

[link] Note that text decorations are not propagated to floating and absolutely positioned descendants, nor to the contents of atomic inline-level descendants such as inline blocks and inline tables.

So you could also use float: left or position: absolute

CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • very close, but if you put it in a fiddle and play around with it you'll see the problem – David Z Jan 08 '16 at 19:56
  • intention was for the "blocking irritating text' to underline on hover separately, here when you hover over it, the whole text gets underline - doesn't look like I can get it done with CSS :( – David Z Jan 08 '16 at 20:03
  • oh yeah. you're right. thats the issue even I am facing – Sushil Jan 08 '16 at 20:03
  • @DavidZ its realy hard to get what you mean. but i understand. I know how to solve it, by I have no time right now – CoderPi Jan 08 '16 at 20:05
  • @CodeiSir sorry about that, well if you can solve it with pure CSS you're really pretty good at this – David Z Jan 08 '16 at 20:07
  • @DavidZ as far as i know it should not be possible in css3 but in css4. bin with JS its easy – CoderPi Jan 08 '16 at 20:08
  • @CodeiSir I'm actually having a ton of trouble just trying to add tags around the unwrapped text using jQuery - for one, there are multiple instances of the element with the class, so insertBefore or insertAfter won't work... another solution below conflicted with another script – David Z Jan 08 '16 at 20:14
  • @DavidZ loop through the nodes of the element, check if its a text node (`.nodeType` === 3) and if it's not a blank node (.trim() on its content and check if its not empty) and then wrap this in the new span – CoderPi Jan 08 '16 at 20:17
1
.slide-title a {text-decoration:none;}
.slide-title a:hover {text-decoration:underline;}

$("h1.slide-title").each(function(){
    $(this).contents().each(function(){
    if (this.nodeType === 3 /*text node*/) {
      $(this).wrap("<a></a>");
    }
  });
});

see fiddle: https://jsfiddle.net/cvn43ncg/1/

andi
  • 6,442
  • 1
  • 18
  • 43
  • I used `` as a wrapper for simplicity, but you could use any element/class and then add to the CSS accordingly. – andi Jan 08 '16 at 19:19
  • Its an easy fix, but your version doesnt underline when he hovers on the entire h1 element as his original requested. – Cam Jan 08 '16 at 19:27
  • Also if he has many i would run .each() to do it for everyone of them. – Cam Jan 08 '16 at 19:28
  • Could someone edit the answer to include the .each()? I have quite a few – David Z Jan 08 '16 at 19:31
  • Anyways I couldn't get this code to work on my site for some reason - it didn't add the tags that I selected - and it also broke some other functions that I needed – David Z Jan 08 '16 at 19:37
  • could you provide a link to your site? I can add the .each() - one sec. – andi Jan 08 '16 at 19:39
  • Just updated the code and the fiddle. I'll take a look at your site. – andi Jan 08 '16 at 19:42
  • actually getting very close to the solution, I couldn't use insertBefore, etc because they were many tags of the same class and it would basically insert ALL the tags rather than just the one after it – David Z Jan 08 '16 at 19:42
  • @andi looks like your code is conflicting with an existing jQuery script, not too sure how or why... any work arounds? love the idea though – David Z Jan 08 '16 at 19:52
  • Do you get any error messages in the error console, to give a clue? – andi Jan 08 '16 at 19:54
  • No errors, I have your code live on the site and I've purged caches, do a hard refresh and take a look yourself :) slider is totally messed up lol, and tag not inserted – David Z Jan 08 '16 at 20:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100185/discussion-between-andi-and-david-z). – andi Jan 08 '16 at 20:08
  • if you add a class to the inserted tag, remember to use a single ' instead, using " broke it – David Z Jan 08 '16 at 20:17
1

Quick and easy way that works is to run a jQuery code such as this.

$('.slide-title').each(function() {
  var txt = $(this).find(".slider-category").html();
  $(this).find("span").remove();
  var ex = $(this).text();
  $(this).empty();

  $(txt+"<span class='extra-txt'>"+ex+"</span>").appendTo(this);
});

Add CSS such as

h1.slide-title:hover .extra-txt {
  text-decoration: underline;
}

h1.slide-title a {
  text-decoration: none;
}

h1.slide-title:hover a {
  text-decoration: none !important;
}

Here is a working version.

https://jsfiddle.net/mjtmtsa5/3/

Cam
  • 1,884
  • 11
  • 24
  • Could you modify your code to simply insert the span tags to the unwrapped text and leave the original span tag alone? – David Z Jan 08 '16 at 19:59
-1

Try this :

$('h1').hover(function()
{
  $(this).contents().filter(function() {  
    return this.nodeType == 3 ? $(this).wrap('<u></u>') : null;  // nodeType == 3 returns the inner text of the target element
  });
});

Example : http://jsfiddle.net/DinoMyte/bvtngh57/151/

DinoMyte
  • 8,737
  • 1
  • 19
  • 26