Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS border in JavaScript

I am using below procedure to modify CSS from JavaScript but it is not giving any result.

Can anybody please check the code and let me know the proper method. I need border for the table with radius.

This is my table structure:

<table id="tt" width="400" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="179" class="header_links">5<input name="input" class="lang_textbox" type="text" value="Search by keyword" /></td>
        <td width="52" align="left"><img src="images/search_go.jpg" width="28" height="24" alt="go" /></td>
        <td width="169" class="header_links"><a href="#">FAQs</a> | <a href="#">Sitemap</a> | <a href="#">Contact us</a></td>
      </tr>
    </table>

And below is the javascript which am using

document.getElementById('tt').style.borderRadius = '4em'; // w3c
document.getElementById('tt').style.MozBorderRadius = '4em'; // mozilla
document.getElementById('tt').style.border = '4em'; // mozilla
like image 262
Sowmya Avatar asked Nov 18 '11 12:11

Sowmya


People also ask

How do you add a border without CSS in HTML?

Using Inline Style attribute Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the inline property for adding the border. Step 2: Now, place the cursor inside the opening tag of that text around which we want to add the border.

How do I put a border around the whole page in CSS?

If you want a border around the entire page, put that border property within body{} in your CSS.


2 Answers

You've got to set the border itself (and note border is not a Mozilla-only property):

document.getElementById('tt').style.border = '4em solid black';

http://jsfiddle.net/KYEVq/

like image 199
Jared Farrish Avatar answered Oct 11 '22 08:10

Jared Farrish


set CSS style in js solutions all in one

  1. set each style property separately

const app = document.querySelector(`#app`);

// set each style property separately
app.style.borderRadius = '4em';

app.style.border = '1px solid red';
// app.style.border, equals to

/*
app.style.borderWidth = '1px';
app.style.borderStyle = 'solid';
app.style.borderColor = 'red';

*/
<div id="app">
 text content...
</div>
  1. set all style properties in once

const app = document.querySelector(`#app`);

// set all style properties in one class
app.style = `
  border-radius: 4em;
  border: 1px solid red;
`;
<div id="app">
 text content...
</div>
  1. set all style properties in one class, with setAttribute API

const app = document.querySelector(`#app`);

// set all style properties in one class
app.setAttribute(`class`, `app-style-class`)
.app-style-class{
  border-radius: 4em;
  border: 1px solid red;
}
<div id="app">
 text content...
</div>
  1. set all style properties in one class, with classList API

const app = document.querySelector(`#app`);

// set all style properties in one class
app.classList.add(`app-style-class`)
.app-style-class{
  border-radius: 4em;
  border: 1px solid red;
}
<div id="app">
 text content...
</div>

refs

https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute

https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

see more

https://www.cnblogs.com/xgqfrms/p/13932298.html

like image 25
xgqfrms Avatar answered Oct 11 '22 10:10

xgqfrms