Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with innerHTML and DIV

Status
Not open for further replies.

hedlro

Programmer
May 18, 2005
3
GB
Hi Guys,

I was wondering if any of you could help.

I have a div that I update with innerHTML (yes I know it is frowned upon). Upon the update there is an ajax call that get the relevant data from the db. Then, and this is where the problem is, i need to access the contents I have placed in the div and update it with the data I have got from the DB. I just cant seem to access the contents I have placed in the Div back out again once I have put them in.

I am an experienced javascript developer but I really can't work it out. I am going insane!!! Would it be better to create a new object within the DIV???

Any advice would be very welcome.

Rob
 
If you can place it into the innerHTML you should be able to get it out. The Property is them same, its readable and writable.

As for the Update, that depends on what you want to do. if you want to add content, replace content, or modify content.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
hedlro said:
I have a div that I update with innerHTML (yes I know it is frowned upon).

Quite the opposite... It's the fastest way of getting markup into the DOM, and as far as I know, completely cross-browser compatible.

As already mentioned, if you can access the DIV once to put the content in, there is no reason at all (*) why you cannot repeat this same operation to update the content again.

* - Assuming, of course, that the original DIV still exists (e.g. you're not re-creating or moving the DIV but trying to use an existing pointer to it).

Again, as mentioned already, some code or a URL would be good.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Another hunch: Are you using document.write (or document.writeln) after the page has loaded? That would cause any markup loaded with the page to be lost (although you probably know that already, so apologies for asking the obvious!).

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi Guys,

Thanks for the replies so far. Regarding posting the code, unfortunately this is a rather complicated drag and drop system where elements can be picked up, dragged and dropped into containers with the mouse, so it is a no go in copying the code as we are talking about 1000 lines. I am however creating a concise version of what happens so i can post that up here.

To vacunita: I know what you are saying as i thought the same. I am putting the information into the layer, it displays on the screen but when I try to get it back out using innerHTML or loop through all elements within the div it is empty???

To BillyRayPreachersSon: Glad to here that, i am a great believer in not over complicating code and the coders that preach that you have to create an element, bind the element, update the element etc etc etc where a simple innerHTML will do. The comment I made of it being frowned upon was so I didnt get a barrage of insults from the self proclaimed code police!!

Will post cut down code asap

Thanks

Rob
 
Another hunch: Are you trying to use getElementById (or some other node retrieval method) where your ID (or CSS selector, etc) has "odd" characters (e.g. "[" or "]") in it?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Something as simple as:

document.getElementById('mydiv').innerHTML

Should be all it takes.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi guys,

thanks for the help so far. I think I have solved it. My code did a function call after the element was dropped into the div. Within the function was the problem code containing the innerHTML. All I simply did was bring this code inline with the section that did the function call and it sort of worked. I saw sort of as I appear to have a problem with my ajax call but I will sort that out (hopefully).

I really cant explain why the code would not work when contained within a function but the same code worked perfectly when inline????

Anyway guys thanks for all your help.
Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top