0

In my project, I have a parent div and inside the parent div, I have three child elements. I want to hide all the child elements except for the first one. so I tried CSS and jquery kind of things, it's working fine but in the middle of that child element I have few strings(special characters) there (like this - ' | ', ' - '). those things are not hiding. any other ways to hide that element? CSS or JQuery?

jQuery(document).ready(function(){
  jQuery(".login_content").children().hide();
  jQuery('.welcome').show();
});
.login_content > * {
    display: none;
}
.login_content > .welcome {
    display: flex;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="login_content">
  <div class="welcome">Welcome <b> name</b></div>
     <b><a href="/">Agent Portal</a></b> | 
     <b><a href="/">Edit profile</a></b> - 
     <b><a href="/logout">Sign out</a></b>
</div>

this is my code, please help me to fix this.

Sankar S
  • 207
  • 3
  • 12
  • Why didn't you wrap special characters (- '|', '-') in a tag? For example, in a `span`. – s.kuznetsov Dec 11 '20 at 05:48
  • @sergey kuznetsov, actually I am using the Freshdesk ticker portal. so in there this data is coming. – Sankar S Dec 11 '20 at 06:00
  • @swati thanks for your help. but in your URL those content just hide, but those place still present I mean DOM. I want those elements should not take any place in DOM. – Sankar S Dec 11 '20 at 06:06
  • In your example, the String parts, or the Text Nodes, are not elements. So you cannot use a Child selector. – Twisty Dec 11 '20 at 06:13

3 Answers3

1

if you can't changes in html structure then You can try using font-size

jQuery(document).ready(function(){
  jQuery(".login_content").children().hide();
  jQuery('.welcome').show();
});
.login_content {
  font-size: 0;
}
.login_content > b {
    display: none;
}
.login_content > .welcome {
    display: flex;
    font-size: 16px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="login_content">
  <div class="welcome">Welcome <b> name</b></div>
     <b><a href="/">Agent Portal</a></b> | 
     <b><a href="/">Edit profile</a></b> - 
     <b><a href="/logout">Sign out</a></b>
</div>

or using visibility value

jQuery(document).ready(function(){
  jQuery(".login_content").children().hide();
  jQuery('.welcome').show();
});
.login_content {
  visibility: hidden;
}

.login_content > .welcome {
  visibility: visible;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="login_content">
  <div class="welcome">Welcome <b> name</b></div>
     <b><a href="/">Agent Portal</a></b> | 
     <b><a href="/">Edit profile</a></b> - 
     <b><a href="/logout">Sign out</a></b>
</div>
Minal Chauhan
  • 6,025
  • 8
  • 21
  • 41
1

You can try contains value. I think this working on only one tag.

<h1>Welcome to My Homepage</h1>

<p class="intro">My name is Donald.</p>
<p>I live in Duckburg.</p>
<p>My best friend is Mickey.</p>

Who is your favourite:
<ul id="choose">
  <li>Goofy</li>
  <li>Mickey</li>
  <li>Pluto</li>
</ul>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p:contains(is)").css("background-color", "yellow");
});
</script>

This is W3school demo available Please check

Hardik Godhani
  • 461
  • 3
  • 16
0

You can try the changing visibility value.

.login_content {
  visibility: hidden;
}

.login_content .welcome {
  visibility: visible;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="login_content">
  <div class="welcome">Welcome <b> name</b></div>
  <b><a href="/">Agent Portal</a></b> |
  <b><a href="/">Edit profile</a></b> -
  <b><a href="/logout">Sign out</a></b>
</div>

Edit: A downside of this would be that the hidden elements will still take some space in DOM.

Debsmita Paul
  • 1,442
  • 9
  • 22
  • thank u so much. but I need without taking space from DOM. any other ways? – Sankar S Dec 11 '20 at 06:07
  • can you use jQuery in your project? Then the way I can think of is that you inject a wrapper around the `Agent Portal` and `Sign Out` and then add `display: none` to that wrapper. – Debsmita Paul Dec 11 '20 at 06:09
  • that's a good idea. but I have three children so how I can make a div around three? – Sankar S Dec 11 '20 at 06:12