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 06-16-2008, 10:55 AM   PM User | #1
rf9
New Coder

 
Join Date: Jun 2008
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
rf9 is an unknown quantity at this point
getElementbyId targetting div

Hello, first post here and also fairly new to javascript so please excuse any lameness. I'm using my Actionscript knowledge as the basis for my javascript, they seem pretty similar.

I'm trying to select a div that has been generated by javascript. I'm not sure where I'm going wrong. I have 5 divs named title0, title1 and so on and I want to target one using the variable i.

The last line does not work, it returns null or no properties, maybe my syntax is wrong. I kinda made it up
Code:
var i = 0;
var setProperties = function() {
     alert(i); // this works
     var userInput = document.getElementById('element_label').value;
     document.getElementById('title' + [i]).innerHTML = userInput;
}
If I change the last line to this then the targetting works:
Code:
document.getElementById('title' + 1).innerHTML = userInput;
Any advice much appreciated.

Last edited by rf9; 06-16-2008 at 11:57 AM..
rf9 is offline   Reply With Quote
Old 06-16-2008, 11:17 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
try
Code:
for(var i=0;i<5;i++)
 document.getElementById('title'+i).innerHTML = 'somtext'+i;
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 06-16-2008, 11:55 AM   PM User | #3
rf9
New Coder

 
Join Date: Jun 2008
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
rf9 is an unknown quantity at this point
thanks abduraooft,
The code you posted does work the only problem is it changes the text for all 5 divs. My fault as I didn't really give enough explanation in my first post.

I only want to change the text for one div at a time so if 'i' is 3 then the text for title3 will be updated and so on.

Thanks

edit: just noticed an error in my first post ('title' = [i]) should read ('title' + [i]). Original post now corrected.
rf9 is offline   Reply With Quote
Old 06-16-2008, 01:10 PM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
var i = 0;
var setProperties = function() {
alert(i); // this works
var userInput = document.getElementById('element_label').value;
document.getElementById('title' + [i]).innerHTML = userInput;
}
It should be
Code:
document.getElementById('title' +i).innerHTML = userInput;
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 06-16-2008, 01:13 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
Which is the event and when should that function be triggered? Can you post the HTML code as well?
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 06-18-2008, 12:35 PM   PM User | #6
rf9
New Coder

 
Join Date: Jun 2008
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
rf9 is an unknown quantity at this point
Thanks for the replies, I figured out a solution now so I thought I'd post it.
Code:
//Set value to pass to setProperties function
var showControlPanel = function(count) {
	document.getElementById('element_label').setAttribute('onKeyup', 'setProperties('+count+')');
}
Code:
// count used for increment
var setProperties = function(count) {
	var userInput = $('element_label').value;
	document.getElementById('title'+count).innerHTML = userInput;
}
Thanks
rf9 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 09:38 PM.


Advertisement
Log in to turn off these ads.