Tag Archives: div

Javascript: Display a second drop-down menu or div based on user’s selection in first drop-down menu

Here is the Javascript that I use to display a second drop-down menu or a div based on a user’s selection in the first drop-down menu.
Read more …

WordPress Tricks: Add a div or other element to one specific page or page type

 

Here is the code I use to add a div or image or other element to just one page or category page of my WordPress sites:

This code will add a div with the id “featured_content” only on the home page:

<?php if ( is_home() ) { ?>
<div id="featured_content">
&nbsp;
</div>
<?php } ?>

 

This code would add a div with the id “featured_recipe” to all posts in the category “recipes.”:

<?php if ( is_category('recipes') ) { ?>
<div id="featured_recipe">
&nbsp;
</div>
<?php } ?>

 

This code would exclude “myimage.jpg” only on the Contact page:

<?php if ( !is_page('contact') ) { ?>
<img src="myimage.jpg">
<?php } ?>

 

As you can see, there are many uses and ways implementations of this code. Let me know if you have any questions or need further assistance.

CSS Tricks: How to make sure an image is always centered inside its div

Here is the code I use to keep an image always centered inside its containing div, regardless of browser width or resizing:

<img src="yourimage.jpg" style="width:100%;margin:0 auto;">

How to center a UL element inside a div

Here is the simple code you can use to center a <ul> element inside a div:

ul {
 width:200px;
 margin:0 auto;
 }

The biggest thing to make sure of is that your width is an exact width, not a percentage.

Let me know if you have any questions or run into any issues.

Use Javascript to open all links within a certain div in a new window

Here is the code that I use to set all the links within a certain div of a web page to open in a new window:

<script type="text/javascript">
var linktarget="_blank" //Specify link target added to links when set to open in new window
var formcache=document.targetmain
function applywindow(){
 if (typeof targetlinks=="undefined") return
 if (!formcache || (formcache && formcache.targetnew.checked)){
 for (i=0; i<=(targetlinks.length-1); i++)
 targetlinks[i].target=linktarget
 }
 else
 for (i=0; i<=(targetlinks.length-1); i++)
 targetlinks[i].target=""
 }
function collectElementbyClass(){
 if (!document.all && !document.getElementById) return
 var linksarray=new Array()
 var inc=0
 var alltags=document.all? document.all : document.getElementsByTagName("*")
 for (i=0; i<alltags.length; i++){
 if (alltags[i].className=="your-link-class")
 linksarray[inc++]=alltags[i]
 if (alltags[i].className=="your-div-class"){
 var alldivlinks=document.all? alltags[i].all.tags("A") : alltags[i].getElementsByTagName("A")
 for (t=0; t<alldivlinks.length; t++)
 linksarray[inc++]=alldivlinks[t]
 }
 }
 return linksarray
 }
 if (formcache && formcache.targetnew.checked) //overcome IE bug, manually check checkbox that has "checked" attribute
 setTimeout("document.targetmain.targetnew.checked=true",100)
 var targetlinks=collectElementbyClass()
 applywindow()
</script>

 

Copy and paste that code into your web page just before the closing < body > tag.

Next, change your-link-class and your-div-class in the code to the class of the links or divs whose links you want to open in a new window.

Finally, simply add the class your-link-class (or whatever you changed it to) to any links that you want to open in a new window OR add the class your-div-class (or whatever you changed it to) to the div whose links you want to open in a new window.

For example: if you change your-link-class to blank, you would add this class to each link you want to open in a blank window:

<a href="http://yourwebsite.com" class="blank">Your Link</a>

And if you change your-div-class to blank-div, you would add the class to the entire div whose links you want to open in new windows:

<div id="mydiv" class="blank-div">
<a href="http://firstwebsite.com">Link #1</a>
<a href="http://secondwebsite.com">Link #2</a>
<a href="http://thirdwebsite.com">Link #3</a>
</div>

Each link in this div will automatically open in a new window.

Credit for this script goes to Dynamic Drives, the article where I found this code is at http://www.dynamicdrive.com/dynamicindex8/newwindow3.htm.

WordPress: How to show a div on only certain pages

Here is the code you can use to show a <div> on only the pages that you want it to be shown on:

<?php if( is_page(home) ) { ?> 
<div> This is the div that will only show up on the page named 'Home' </div> 
<?php } ?>

You can also have it show up on every page except for a particular page:

<?php if( !is_page(home) ) { ?>
<div> This is the div that will only show up any page that is NOT named 'Home' </div> 
<?php } ?>

Let me know if you have any questions or trouble with this at all.