Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-28-2011, 11:19 AM   PM User | #1
ktelfon
New to the CF scene

 
Join Date: Sep 2011
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
ktelfon is an unknown quantity at this point
Backcolor changing. Simple stuff.

So how do i get to backgroundColor value to change it?

a.backgroudColor('black');

or how ?

Code:
var a = makeDiv(p.age , {
    backgroundColor: 'gray',
    color: 'white',         
    padding: '2px 6px',    
    fontSize: '15pt',       
  });
ktelfon is offline   Reply With Quote
Old 09-28-2011, 12:03 PM   PM User | #2
jassi.singh
Regular Coder

 
Join Date: Sep 2011
Posts: 103
Thanks: 0
Thanked 14 Times in 14 Posts
jassi.singh can only hope to improve
Code:
div#nav a {
	display:block;
	color:#1084aa;
	text-decoration:none;
	padding:.3em 1em;
	margin:0 0 2px;
	font:1.2em bold;
	text-align:center;
	}
	
div#nav a:hover {
	color:#000000;
	background-color:#1084AA;
	}
Code:
<a href="#" onclick="changeTab('home','hcontent');" id="home">Home</a>
Code:
var lastTab = "home";
function changeTab(tab,section){
	document.getElementById(lastTab).innerHTML = 
	document.getElementById(lastTab).style.color="#1084AA";
	document.getElementById(tab).style.backgroundColor="#1084AA";
	document.getElementById(tab).style.color="#000000";
	lastTab = tab;
	document.getElementById('content').innerHTML = document.getElementById(section).innerHTML;
}
jassi.singh is offline   Reply With Quote
Old 09-28-2011, 12:07 PM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
It depends on how the function makeDiv() looks like. Show us. For the moment we see no constructor there, so that we don't know if the variable a is an object or not.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 09-28-2011, 12:19 PM   PM User | #4
ktelfon
New to the CF scene

 
Join Date: Sep 2011
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
ktelfon is an unknown quantity at this point
Quote:
Originally Posted by Kor View Post
It depends on how the function makeDiv() looks like. Show us. For the moment we see no constructor there, so that we don't know if the variable a is an object or not.
Code:
var makeDiv = function(str, style) {
 
  var div = $('<div>');
  div.css(style);
  div.text(str);
  return div;
};
ktelfon is offline   Reply With Quote
Old 09-28-2011, 12:55 PM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
If so:
Code:
a[i].style.backgroundColor='black';
If I am right, the function returns a collection of all the elements with the tag name DIV, thus you need also an index [i], to refer a singular element.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 09-28-2011 at 12:58 PM..
Kor is offline   Reply With Quote
Old 09-28-2011, 06:29 PM   PM User | #6
ktelfon
New to the CF scene

 
Join Date: Sep 2011
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
ktelfon is an unknown quantity at this point
Okey i think i didnt show all that is needed so you can help me )

Code:
var makeDiv = function(str, style) {
  
  var div = $('<div>');
      div.css(style);
      div.text(str);
  return div;
};

var makeP = function(p) {
  
  var a = makeDiv(p.name + ' ' + p.surname, {
    backgroundColor: 'gray', 
    color: 'white',          
    padding: '2px 6px',     
    fontSize: '14pt',        
  });

var setS = function(newS){
    sa = newS;
    if (s > 0) {
      a.style.backgroundColor = 'yellow'; //this part or something else dosnt work
    } else {
      s = 0;
      a.style.backgroundColor = 'gray';
    }
  }
I tried this
Code:
$('div#d').css('background-color','yellow');
but div tags dont have an id so i can change all of them but how do i change a specific one ? maybe there is a way to adjust the div creator so it has not only style and text but also an ID.

Last edited by ktelfon; 09-29-2011 at 06:30 AM..
ktelfon is offline   Reply With Quote
Old 09-29-2011, 10:30 AM   PM User | #7
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Quote:
Originally Posted by ktelfon View Post
but div tags dont have an id so i can change all of them but how do i change a specific one ?
If it is a specific one, what makes it specific? Is it something which makes that div unique? You need that to refer it.

DOM Elements can be referred:
- by their id
- by their name, in which case one of them can be refereed by its index within the collection. Anyway, not all the DOM elements are entitled to bear a name.
- by their tag name, in which case one of them can be refereed by its index within the collection
- by their class name, in which case one of them can be refereed by its index within the collection
- by their DOM relationship: parent, child, siblings.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:33 PM.


Advertisement
Log in to turn off these ads.