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

A

adamrfrench

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!
 
A

adamrfrench

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.');
}
}
}
 
W

web.dev

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.');
}
}
}

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;
 
R

RobG

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.');
}
}
}

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.
 
A

adamrfrench

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.
 
R

Randy Webb

(e-mail address removed) 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(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?
 
A

adamrfrench

It would explain even more if it had any context to what you are
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top