473,714 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assigning a variable to "document.getEl ementById('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.getEl ementById('-> 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 10145
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.r eadyState == 4) {
if (http_request.s tatus == 200) {
//alert(http_requ est.responseTex t);
var target = "document.getEl ementById('mysp an').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*********@hot mail.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.r eadyState == 4) {
if (http_request.s tatus == 200) {
//alert(http_requ est.responseTex t);
var target = "document.getEl ementById('mysp an').innerHTML" ;
I'm somewhat confused by what you want to do. Your variable target is
being assigned a string value
"document.getEl ementById('mysp an').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.getEl ementById(" + myVar + ").innerHTM L";

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

var target = document.getEle mentById(myVar) .innerHTML;

Any thoughts?


Oct 19 '05 #3
ad*********@hot mail.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.r eadyState == 4) {
if (http_request.s tatus == 200) {
//alert(http_requ est.responseTex t);
var target = "document.getEl ementById('mysp an').innerHTML" ;
This line will create a local variable called 'target' and assign the
string "document.getEl ementById('mysp an').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.getEle mentById('myspa n').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.getEle mentById(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.getEle mentById('spanI d'); // 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.r eadyState == 4) {
if (http_request.s tatus == 200) {
//alert(http_requ est.responseTex t);
result = http_request.re sponseText;
document.getEle mentById('myspa n').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*********@hot mail.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(t argetID){
if (http_request.r eadyState == 4) {
if (http_request.s tatus == 200) {
//alert(http_requ est.responseTex t);
result = http_request.re sponseText;
document.getEle mentById('myspa n').innerHTML = result;
document.getEle mentById(target ID).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.javas cript 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
1698
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 action. Many thanks!
8
8239
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) { document.location.href = document.location.href.substring(0, document.location.href.indexOf('#')) + '?' + newUrl; } else { document.location.href = document.location.href + '?' + newUrl; }
1
2756
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 item 3
7
8820
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
9952
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 true. var parser = new DOMParser(); var xmlDoc = parser.parseFromString( responseText, "text/xml"); // at this point: xmlDoc= null
2
1560
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
2642
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 iframe's src property. Being that IE does not follow standard and considers an id, name, etc as a qualifying identifier for the document.getElementById object, I double checked to make sure that there's only one instance of id = "servif" and I never use...
2
2016
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); document.getElementById('namedisplay').value=name;
6
2051
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 <script language="javascript"> var form = document.forms; function showNameObject() {
0
8704
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9307
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9170
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9009
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7946
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.