4

how can I hide only the words "Piano Lesson" from this tag using CSS but without an ID or class but using only selectors?

<div class="alert alert-success dynamic-alert ">
    <ul>
        <li>
            (Canceled) <a href="http://www.google.ch">July 18, 2017 13:30</a> Piano Lesson
        </li>
    </ul>
</div>

I am not able to change the html!

Praveen Govind
  • 2,619
  • 2
  • 32
  • 45
iolli
  • 93
  • 2
  • 10

6 Answers6

1

wrap it in span and use internal style.

Note:internal style better than inline style.

.alert li a + span {
  display:none;
}

.alert li a + span {
  display:none;
}
<div class="alert alert-success dynamic-alert ">
    <ul>
        <li>
            (Canceled) <a href="http://www.google.ch">July 18, 2017 13:30</a> <span>Piano Lesson<span>
        </li>
    </ul>
</div>

you said : how could I move the content "(Canceled)" to the place of "Piano Lesson" and replace it?

With jquery :

$(document).ready(function(){
  $('button').click(function(){
    var c = $($('li a')[0].previousSibling).text();
    $($('li a')[0].previousSibling).wrap('<span style="display:none"></style>');
    $($('li a')[0].nextSibling).wrap('<span style="display:none"></style>');
    $('li a').after('<span>' + c + '</span>')
  }) 
})

$(document).ready(function(){
  $('button').click(function(){
    var c = $($('li a')[0].previousSibling).text();
    $($('li a')[0].previousSibling).wrap('<span style="display:none"></style>');
    $($('li a')[0].nextSibling).wrap('<span style="display:none"></style>');
    $('li a').after('<span>' + c + '</span>')
  }) 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="alert alert-success dynamic-alert ">
    <ul>
        <li>
            (Canceled)<a href="http://www.google.ch">July 18, 2017 13:30</a>Piano Lesson
        </li>
    </ul>
    <button>Go</button>
</div> 
Ehsan
  • 12,655
  • 3
  • 25
  • 44
  • That helped! Any how could I move the content "(Canceled)" to the place of "Piano Lesson" and replace it? Thank you! – iolli Jul 18 '17 at 12:07
1

<div class="alert alert-success dynamic-alert ">
    <ul>
        <li>
            (Canceled) <a href="http://www.google.ch">July 18, 2017 13:30</a> <span style="display:none;">Piano Lesson</span>
        </li>
    </ul>
</div>

Just add Piano Lesson inside tags, and use style="display:none;" to hide it.

Edit: You can also use this (No extra ID & Class needed)

.alert ul li span{
display:none;
}
<div class="alert alert-success dynamic-alert ">
    <ul>
        <li>
            (Canceled) <a href="http://www.google.ch">July 18, 2017 13:30</a> <span>Piano Lesson</span>
        </li>
    </ul>
</div>
Kamal Chhirang
  • 490
  • 4
  • 14
1
$('ul li').each(function(){
    var content=$(this).html();
    var check=content.search('Piano Lesson');
    if(check!='-1'){
        content=content.replace('Piano Lesson','');
    }
    $(this).html(content);
})
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Dec 28 '17 at 13:12
0

Follow this code.

Html

<ul>
        <li>
            (Canceled) <a href="http://www.google.ch">July 18, 2017 13:30</a><span> Piano Lesson</span>
        </li>
    </ul>

css

ul li span{display:none}

Or another way:

Html

<ul>
        <li>
            (Canceled) <a href="http://www.google.ch">July 18, 2017 13:30</a><span class="span-style"> Piano Lesson</span>
        </li>
    </ul>

css

ul li .span-style{display:none} or .span-style{display:none}
0

If you can't edit the html...you can still get the desired effect but in a hacky way.

This has no cross browser support yet...and by I mean it won't work on IE or Edge. I tested it in Chrome 59 and FF 54 and all is good there.

With that being said, Can I use says this will work for at least 89% of users.

.alert li {
  -webkit-mask-image: -webkit-linear-gradient(left, rgba(0, 0, 0, 1)12.8em, /** <---- manually edit this value to determine the cutoff point **/
  rgba(0, 0, 0, 0)0, rgba(0, 0, 0, 0));
}
<div class="alert alert-success dynamic-alert ">
  <ul>
    <li>
      (Canceled) <a href="http://www.google.ch">July 18, 2017 13:30</a> Piano Lesson
    </li>
  </ul>
</div>
I haz kode
  • 1,587
  • 3
  • 19
  • 39
0

Altering the HTML is the best option as others have said.

An alternative method if you can't do that:

.alert ul li {
 font-size: 0;
}

.alert ul li a {
 font-size: 16px;
}

.alert ul li:before {
  content: 'Canceled';
  display: inline-block;
  font-size: 16px;
  margin-right: 5px;
}
<div class="alert alert-success dynamic-alert ">
    <ul>
        <li>
            (Canceled) <a href="http://www.google.ch">July 18, 2017 13:30</a> Piano Lesson
        </li>
    </ul>
</div>
sol
  • 22,311
  • 6
  • 42
  • 59