-1

I have this html

<div id="body" role="main">
    <h1>Title</h1>
<p>My site is mysite.com</p></div>

I have <div id="body" role="main"> or <h1> and <p> on every page.

I want to hide inner text of <h1> and <p> or "Title" and "My site is mysite.com" on specific URLs without effecting <div id="body" role="main"> or <h1> and <p> on other pages.

Please help me to hide them with css or js?

Thanks

9
  • not sure what you mean. You want to hide those elements using CSS or JS without referencing div, role or id on #body and without referencing h1 or p in your css/js selectors? Commented Jul 22, 2017 at 15:08
  • Do you want to keep tags <h1></h1> and <p></p> and just remove the inner text? Commented Jul 22, 2017 at 15:08
  • 1
    @miko Would it be fine for you to add a "hidden" class to do this? If so I believe we can help. Unless you use JavaScript/jQuery, there's no other way unless using a class of some sort. You could also add it in inline styling but your question doesn't completely make sense. Please clarify further. Commented Jul 22, 2017 at 15:11
  • @Michael Coker - yes Commented Jul 22, 2017 at 15:17
  • @Commercial Suicide - YES Commented Jul 22, 2017 at 15:24

7 Answers 7

1

You can hide immediate child element's of body:

body > h1, body > p {
   display: none;
}

Le me know if it works for you.

Sign up to request clarification or add additional context in comments.

Comments

1

Just use .innerText = ''; to set empty content. Check the snippet below.

Update : To check the URI and only apply if it matches, use

var uri = window.location.pathname.substr(1).replace('/', '');
if (uri == 'secret') {
    var div = document.getElementById('body');
    div.getElementsByTagName('h1')[0].innerText = '';
    div.getElementsByTagName('p')[0].innerText = '';
}

var div = document.getElementById('body');
div.getElementsByTagName('h1')[0].innerText = '';
div.getElementsByTagName('p')[0].innerText = '';
<div id="body" role="main">
    <h1>Title</h1>
<p>My site is mysite.com</p></div>

9 Comments

that code will efect to all page?
It depends on where you place the code. If need to hide text only on specific pages, place the above code only on the pages where you need to hide the text
If I am add css or js, the code will load on every page, I want the code just efect in specific URL only like "thesite.com/secret"?
Why can't you edit the "thesite.com/secret" page and place the script ?
Don't place the script inside a .js file which loaded for every page. Place it inside html page between <head> and </head>
|
0

You can use the Css display property like this :

h1{
   display : none;
}

1 Comment

That code will hide all <h1> in all page!
0

You can use display: none;

#YOUR_ELEMENT_ID {
   display: none;
}
#YOUR_ELEMENT_ID {
   display: none;
}

1 Comment

I am don't have permission to modify <h1> and <p> but I am can add css or js.
0

To remove the h1 and p from the display on /secret/ you can use javascript. Just add this to the bottom of of /secret/.

<script>
            var target = document.getElementById('body');
            target.querySelector('p').style = 'display: none;';
            target.querySelector('h1').style = 'display: none;';
</script>

Or if you have to add JS to all of your pages, you can check the URI and only apply if it matches /secret/

<script>
        var uri = window.location.pathname.substr(1).replace('/', '');
        if (uri == 'secret') {
            var target = document.getElementById('body');
            target.querySelector('p').style = 'display: none;';
            target.querySelector('h1').style = 'display: none;';
        }
</script>

9 Comments

I am have add in all page in <head> but not work? I use "check the URI".
@miko can you add the script to the bottom of the page instead?
@miko this question covers why it isn't working - the first solution has a bunch of methods stackoverflow.com/questions/14028959/…
I am add in above </body> tag, work but just in <h1> first tag not work in other <h1> or <p> tag in same page?
@miko are you defining #body in multiple places on the page?
|
0

i don´t like id so i prefer class.

<style>
.hide {
  display: none;
}
</style>

So your html:

<div id="body" role="main">
  <h1 class="hide">Title</h1>
  <p class="hide">My site is mysite.com</p>
</div>

Hope this will help.

If you can´t change the html class or id and both elements are always at position one and two then use this.

#body > h1:nth-child(1),
#body > p:nth-child(2) {
  display: none;
}

Comments

0

You can use {display:none} style for hide text for a particular tag

H1-1

My site is mysite.com

h1{ display : none}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.