admin管理员组

文章数量:1312782

Is there any way to hide a div from source code at runtime

Using jquery, javascript or any other method.

Eg:

<div id="abc">
This will not be displayed on source as well as in page
</div>

By using jQuery remove() or hide() - the item is hidden from front end. But i need to remove it from source...

I'm using drupal render method.

Is there any way to hide a div from source code at runtime

Using jquery, javascript or any other method.

Eg:

<div id="abc">
This will not be displayed on source as well as in page
</div>

By using jQuery remove() or hide() - the item is hidden from front end. But i need to remove it from source...

I'm using drupal render method.

Share Improve this question edited Oct 17, 2014 at 4:39 ReNiSh AR asked Oct 17, 2014 at 4:33 ReNiSh ARReNiSh AR 2,8522 gold badges33 silver badges43 bronze badges 3
  • 3 then, why do you include the div? xD – manix Commented Oct 17, 2014 at 4:34
  • it will automatically displayed from render method in drupal... – ReNiSh AR Commented Oct 17, 2014 at 4:36
  • 1 You're likely attacking the problem from the wrong angle. Since you're using Drupal, look into the hooks system. – jprofitt Commented Oct 17, 2014 at 4:47
Add a ment  | 

5 Answers 5

Reset to default 6

It is not possible to hide DOM elements in browser. You can only remove them using .remove() or hide with .hide() when rendered. But if DOM exist in code then you can not hide it in view source code ponent.

Here is one solution

In your body tag add onload event

<body onload='removeDiv()'>
    <div id='abc'>
         This will not displayed in source code as well as in web page.
    </div>
<body>

Javascript

<script>
    function removeDiv(){
        var div = document.getElementById('abc');
        div.remove();
    }
</script>
<script>
    $('.abc').hide();
</script>

If the .hide() and .remove() doesn't work for you. You can try this. Make a parent div and set the html part empty

  <div id="def">
     <div id="abc">
        This will not be displayed on source as well as in page
     </div>
  </div>

  <script type="text/javascript">
       $("#def").html('');
  </script>

If you are using drupal the write a php if condition to hide the content of the div, example

<?php if(1){ ?>
<div id="abc">
This will not be displayed on source as well as in page
</div>
<?php }?>

本文标签: javascriptIs there any way to hide a div from source code at runtimeStack Overflow