472,338 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,338 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 9915
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...
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('?')); ...
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...
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">...
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...
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...
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...
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 =...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.