By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,474 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,474 IT Pros & Developers. It's quick & easy.

updating values with getelementsbyid

P: 10
Hi everyone,

I am trying to update the value which is stored in a particular part of my page by using the getElementsById method, followed by calling a function to update the field. However, I donít know how to do this. The relevant parts of my code are as follows:

Expand|Select|Wrap|Line Numbers
  1. function text(t)
  2.     {
  3.       return document.createTextNode(t);
  4.     }
  5. function start(){
  6.  
  7. var oHelloText = text(getQuestion()); //the getQuestion method returns a string from an array
  8.         oHelloText.id = "test";
  9. }
  10.  
  11. function updateDetails()
  12.     {
  13.         document.getElementById('test') = text(getQuestion());           
  14.     }
  15.  
//sometime later, call to updateDetails method.

Unsurprisingly, this doesnít work.

Any advice?
Nov 8 '07 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,390
hi ...

as far as i can see you create a textnode from a string. but with that you cannot do:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('test') = text(getQuestion());
you cannot work with dom-nodes that way i think. you simply could do:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('test').value = 'your_text';
or you should have a look at the replaceChild() method ... the text of your element to replace is a textnode with a textvalue itself ... so you have to replace it accordingly ...

kind regards
Nov 8 '07 #2

P: 10
I see.

I went a layer up and gave the id to my P, which contains the text node.

I then wrote this (where 'test' now refers to the holder of the text node).

Expand|Select|Wrap|Line Numbers
  1. function updateDetails()
  2.     {
  3.         var zzz = (document.getElementById('test'));
  4.         zzz.removeChild(zzz.firstChild);
  5.         zzz.appendChild(text(getQuestion()));
  6.  
  7.     }
This works, but doesn't seem very efficient. Is there a more suitable way?
Nov 8 '07 #3

gits
Expert Mod 5K+
P: 5,390
hi ...

why do you use the complex textNode-way? you could set the value ... is there any reason that deserves the node-way for you?

btw: please use code-tags when posting source code ...

kind regards
Nov 8 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.