473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Assigning a variable to "document.getElementById('VAR').innerHTML"

Let it be mentioned that Javascript is not my forte, so the solution to
this could very well be a simple one.

I am working on an AJAX function where I can pass a URL and the target
ID in, and have the function update the target ID with the URL. There
is a bit more to it then that, but that is the basics.

my difficulty comes when I try to assign a variable to:
"document.getElementById('-> var gose here <-').innerHTML"

I have tried a number of different possibilities (eval, just breaking
it out, ect) with no luck. I get an 'Object expected' error when I try
to alert() the var, as well as when I try to use it in other functions.

so, to sum it up, I just need to know how to be able to get a variable
in there! Thanks fer all yer help!

Oct 19 '05 #1
6 10080
upon furthur inspection, it looks as though my problem isen't assigning
the variable, but rather haveing the statment interpret it as an object
(Kinda daft on me considering the error message). Here is my code.
------------------------------------------------------------------------------------
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
var target = "document.getElementById('myspan').innerHTML";
target = result;
} else {
alert('There was a problem with the request.');
}
}
}
---------------------------------------------------------------------------------------
My objective is to be able to set 'myspan' as a variable.

Any thoughts?

Oct 19 '05 #2

ad*********@hotmail.com wrote:
upon furthur inspection, it looks as though my problem isen't assigning
the variable, but rather haveing the statment interpret it as an object
(Kinda daft on me considering the error message). Here is my code.
------------------------------------------------------------------------------------
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
var target = "document.getElementById('myspan').innerHTML";
I'm somewhat confused by what you want to do. Your variable target is
being assigned a string value
"document.getElementById('myspan').innerHTML".
So either you actually want to get a value from the innerHTML or you
actually want that entire string.
target = result;
} else {
alert('There was a problem with the request.');
}
}
}
---------------------------------------------------------------------------------------
My objective is to be able to set 'myspan' as a variable.
If I'm understanding you correctly, then you have some variable with a
string value of 'myspan'. Then the following would suffice:

var myVar = "myspan";

var target = "document.getElementById(" + myVar + ").innerHTML";

Again, I'm not sure if you're asking that the whole value to be a
string or not.

var target = document.getElementById(myVar).innerHTML;

Any thoughts?


Oct 19 '05 #3
ad*********@hotmail.com wrote:
upon furthur inspection, it looks as though my problem isen't assigning
the variable, but rather haveing the statment interpret it as an object
(Kinda daft on me considering the error message). Here is my code.
------------------------------------------------------------------------------------
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
var target = "document.getElementById('myspan').innerHTML";
This line will create a local variable called 'target' and assign the
string "document.getElementById('myspan').innerHTML" as its value.

Guessing that 'myspan' is the id of a span element that you want to get
the content of, then there is some HTML that looks like:

<span id="myspan" ...> ... </span>

then:

var target = document.getElementById('myspan').innerHTML;

will do.

On the other hand, if:

- 'myspan' is a global variable (it is not declared within the function
so for it to have any value it must be given one somewhere else, and
for it to have that value inside this function it must be global
since you haven't passed it to the function)

- 'myspan' has as its value the ID of a span in the current document,
e.g. somewhere there is a bit of HTML like:

<span id="spanId" ...> ... </span>

and somewhere else a bit of JavaScript like:

myspan = 'spanId';

- that you want to assign the HTML content of 'myspan' to the variable
'target'

then:

var target = document.getElementById(myspan).innerHTML;

will do.

target = result;
This immediately replaces the value of target with the value of
'result'. For result to have a value, it must be a global variable (you
have not revealed how it was defined or given a value).

If 'result' has not been declared then it will cause an error (its
status is 'not defined', which is different to 'undefined').

} else {
alert('There was a problem with the request.');
}
}
}
---------------------------------------------------------------------------------------
My objective is to be able to set 'myspan' as a variable.
var myspan; // undefined
var myspan = 'whatever'; // String
var myspan = 4; // Number
var myspan = {}; // Object
var myspan = document.getElementById('spanId'); // element reference
// and so on and so forth, et cetera, et cetera...


Any thoughts?


It's a lovely day - despite (or maybe because of) the rain.

--
Rob
Oct 19 '05 #4
I think this piece of code might explain a bit more.
---------------------------------------------------------------------------
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('myspan').innerHTML = result;
} else {
alert('There was a problem with the request.');
}
}
}
---------------------------------------------------------------------------
Pritty common accross the net. What I am trying to acomplish is this. I
want to be able to specify the target ID (in this example 'myspan') by
passing an argument into the function. Therefor what is currently set
as 'myspan' needs to become a variable. The variable will be global
because the variable will not be passed into THIS function, but from
another which will alter that variable.

Oct 20 '05 #5
ad*********@hotmail.com said the following on 10/19/2005 9:30 PM:
I think this piece of code might explain a bit more.
It would explain even more if it had any context to what you are
replying to. This is Usenet, please quote what you are replying to.
---------------------------------------------------------------------------
function alertContents() {
function alertContents(targetID){
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('myspan').innerHTML = result;
document.getElementById(targetID).innerHTML = result;
} else {
alert('There was a problem with the request.');
}
}
}
---------------------------------------------------------------------------
And then call it as something like this:

alertContents('elementID')
Pritty common accross the net.
eval and other crappy scripts are "common across the net" also. It
doesn't make them worth having though.
The variable will be global because the variable will not be passed into
THIS function, but from another which will alter that variable.


In that case, then just make targetID a global variable. But, why not
pass it as an argument and keep the global namespace clear of it?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 20 '05 #6
>>>It would explain even more if it had any context to what you are
replying to. This is Usenet, please quote what you are replying to.


I'll try to do better next time. In regards to context. What I was
trying accomplish (and finally have accomplished) was a kind of
"catch-all" AJAX script. I wanted a single function that could handle
forms (using POST) as well as GET and straight-forward links. I wanted
to be able to specify the form, the HTML (or PHP) file, as well as the
div ID tag from this single function.

And it finally works. I suspect that this ain't much for a skilled hand
at Javascript, but for me, who has avoided Javascript as much as
possible, its a bit of an achivement.

Thanks again for all your help.

Oct 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jack3000 | last post by:
Is there anyway to view what is written when using document.write? When I view the source from the webbrowser, I don't see the code that is written after the browser performs the document.write...
8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
1
by: Richard | last post by:
http://dynamicdrive.com/dynamicindex1/switchmenu.htm I want to add a second level menu item to the existing design. Currently only one level is possible. Item 1 ......link ......link item 2...
7
by: Phin | last post by:
Hi, I am tring to update some text in a div statement via document.getElementById("test").innerHTML = "data loaded!" and <div id="test"> Loading ... please hold!
3
by: sfeher | last post by:
Hi All, The following code returns a valid xmlDoc (since I can evaluate and selectNodes) but its value is "xmlDoc= null" ?! Or at least this is what the FireBug shows and (xmlDoc===null) is...
2
by: Bryan | last post by:
Hello all, Can anyone explain when one should use the "document" object and when one should use the "this" object? Also, is the "self" object the same as the "document" or "this" object?
4
by: dr1ft3r | last post by:
Hey guys, I'm building a site for a landscaping business down the street and can't seem to get part of the code functioning correctly. The code fails on line 68 where I make a reference to an...
2
by: ratcateme | last post by:
I have this script function getname(){ <script type="text/javascript"> var name = prompt('What is your name?'); while(name==''){ name = prompt('We need your name?'); } alert(name);...
6
by: maminx | last post by:
hello all, i have this html below <input type="text" name="items" size="3" value="1" onchange="javascript:showNameObject();"/> and i want to alert that object of name with this script ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.