Jump to content

Rounded corners using javascript


Wolphie

Recommended Posts

I'm trying to create rounded corners using javascript and CSS. The reason i'm using javascript is so i don't have un-needed markup in my code.

 

Javascript:

function roundedCorners() {
  var divs = document.getElementsByTagName('div');
  var rounded_divs = [];
  for(var i = 0; i < divs.length; i++) {
    if(/\brounded\b/.exec(divs[i].className)) {
      rounded_divs[rounded_divs.length] = divs[i];
    }
  }
  /* Now add additional divs to each of the divs we have found */
  for(var i = 0; i < rounded_divs.length; i++) {
    var original = rounded_divs[i];
    /* Make it the inner div of the four */
    original.className = original.className.replace('rounded', '');
    /* Now create the outer-most div */
    var tr = document.createElement('div');
    tr.className = 'rounded2';
    /* Swap out the original (we'll put it back later) */
    original.parentNode.replaceChild(tr, original);
    /* Create the two other inner nodes */
    var tl = document.createElement('div');
    var br = document.createElement('div');
    /* Now glue the nodes back in to the document */
    tr.appendChild(tl);
    tl.appendChild(br);
    br.appendChild(original);
  }
}
window.onload = roundedCorners;

 

CSS:

div.rounded {
width: 170px;
padding: 15px;
background: #FFFFFF;
text-align: left;
}
div.rounded2 {
width: 200px;
background: #FFFFFF url("images/tr.png") no-repeat top right;
}
div.rounded2 div {
background: transparent url("images/tl.png") no-repeat top left;
}
div.rounded2 div div {
background: transparent url("images/br.png") no-repeat bottom right;
}
div.rounded2 div div div {
background: transparent url("images/bl.png") no-repeat bottom left;
padding: 15px;
}

 

Example HTML:

<div class="rounded">
  This is a test
</div>

 

The output is just a rectangle, with no rounded corners. And i'm not sure why it's doing this. Hopefully somebody can help!  ;D

Link to comment
Share on other sites

First things first, make sure you aren't getting any errors. This is easy in Firefox with the Firebug extension; it should also tell you what the "resultant" HTML is. If you don't have FF, turn on the IE setting that notifies you about script errors.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.