/    Sign up×
Community /Pin to ProfileBookmark

xmlhttprequest onClick – code is appearing in wrong location

I have 2 .js files – x.js and y.js.

The 2 files are the same, except for x.js holds an onClick event function for one button which displays where div id=’xjs’.

the second file y.js holds an onClick event function for another button which displays where div id=’yjs’.

I have a table, in the left column is my div id=’xjs’, and in the right column is my div id=’yjs’.

When I click the first button, the correct text appears in the xjs div, but when I click the second button the text overwrites the xjs div and does not appear in the yjs div area.

Here is the code I’m using from the .js files.

[QUOTE]

var xmlHttp

// Check if browsers support AJAX
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}

// here the div id should = yjs
function stateChanged()
{
if (xmlHttp.readyState == 4 || xmlHttp.readyState == “complete”)
{
document.getElementById(“yjs”).innerHTML = xmlHttp.responseText
}
}

function warpUser(str)
{
xmlHttp = GetXmlHttpObject()

if(xmlHttp == null)
{
alert(“Browser does not support HTTP Request”)
return
}

// call the .php file to do the work
var url = “action.php”
url = url + “?id=” + str
url = url + “&sid=” + Math.random()
xmlHttp.onreadystatechange = stateChanged
xmlHttp.open(“GET”,url,true)
xmlHttp.send(null)
}

[/QUOTE]

I believe the problem to be in the stateChanged() function. But I do not know how to output to multiple areas, only one. I would like to output to multiple areas, that is my goal. Please assist.

Brandon

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@npshmearauthorJan 24.2008 — /bump

*want to bump this to the top in the hopes someone will read it.
Copy linkTweet thisAlerts:
@TJ111Jan 24.2008 — Where is the code for the 'xjs' change?
Copy linkTweet thisAlerts:
@npshmearauthorJan 24.2008 — the code for the xjs.js is the same exact except 2 things are different.


// here the div id should = xjs

function stateChanged()

{

if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")

{

document.getElementById("xjs").innerHTML = xmlHttp.responseText

}

}
[/QUOTE]


and then the php file it changes to is here:


// call the .php file to do the work

var url = "action2.php"

url = url + "?id=" + str

url = url + "&sid=" + Math.random()
[/QUOTE]


They are the exact same thing, except each .js file goes to a different .php file and they are supposed to alter a seperate <div id>

xjs.js outputs to <div id='xjs'>

yjs.js outputs to <div id='yjs'>

Yet, they are both outputting to the same div id, (the one that comes first) on the left column -> <div id='xjs'>

This function is changed to
Copy linkTweet thisAlerts:
@TJ111Jan 24.2008 — You can't have multiple functions with the same name. Either change the name of one of the functions, or pass the div you want updated as an argument.
Copy linkTweet thisAlerts:
@npshmearauthorJan 25.2008 — I've been going over the tutorials, and now I really have an understanding for this framework prototype.

Basically it is like a HUGE header file. You include it, and then you (through the api documents/tutorials) can actually simplify a ton of code in javascript by using the prototype functions!

I fixed about 15 errors I had in my project that I am currently working on, and have brought my code down in size almost 50%

Thank you for this help, I am going to be researching much more in the Prototype documents!

Brandon
×

Success!

Help @npshmear spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...