/    Sign up×
Community /Pin to ProfileBookmark

How do I pass this element as a parameter?

On the last line of this code an element DIV1 is being evaluated by getElementById(). How can I pass this as a variable parameter?

Statechanged() errors if you try to pass a parameter with it.

Thanks
Jeff Roehl

[code]var xmlHttp

function showCustomer(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert (“Your browser does not support AJAX!”);
return;
}
var url=”http://onionsalad.com/search.fwx”;
url=url+”?”+str;
url=url+”&sid=”+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open(“GET”,url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById(“DIV1”).innerHTML=xmlHttp.responseText;
}
}

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@mrhooAug 20.2007 — You have to pass innerHTML a string deconstructed from the object into its html representation. Easiest way is to append it to an empty div and pass the div's innerHTML back for the response text.
Copy linkTweet thisAlerts:
@jroehlauthorAug 20.2007 — >> append it to an empty div and pass the div's innerHTML

Thanks for your answer, I am sure it is spot on.

That sounds more complicated than the original question.

I wouldn't even know where to begin implementing your answer.

>> append it to an empty div

How do I do this?

>> pass the div's innerHTML back for the response text

Stumped again.

Does getElementById() take a variable as a parameter?

Thanks

Jeff Roehl
Copy linkTweet thisAlerts:
@toicontienAug 20.2007 — Change this line:
xmlHttp.onreadystatechange=stateChanged;
To this code:
xmlHttp.onreadystatechange=getStateChanged('DIV1');
Change this block of code from:
function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("DIV1").innerHTML=xmlHttp.responseText;
}
}

To this:
function getStateChanged(el)
{
if (typeof(el) === 'string')
{
el = document.getElementById(el);
}
return function() {
if (xmlHttp.readyState==4)
{
el.innerHTML = xmlHttp.responseText;
}
};
}

This utilizes what's called a function closure. The getStateChanged function returns a function object, which is called by the XML HTTP request object. Any data you pass to the getStateChanged function becomes available to the function object returned by getStateChanged. This is handy when the scope in which a function executes gets changed, like in the onreadystatechange function of an XML HTTP request.

The getStateChanged function can take either an HTML tag Id or a node reference to an HTML tag. These two bits of code do the same thing:
xmlHttp.onreadystatechange=getStateChanged('DIV1');

var updateDiv = document.getElementById('DIV1');
xmlHttp.onreadystatechange=getStateChanged(updateDiv);
Copy linkTweet thisAlerts:
@jroehlauthorAug 20.2007 — WooHooo!

I am going to try this. If I can pass the URL parameter(s) and the name of the <div> both as variables then I can just start tossing stuff from the servers database into the html just about anywhere at will.

I changed a couple of things, do you think the following will work?

First I pass showcustomer() 2 variables, adding str2. Then I pass getStateChanged the str2. Do you think this will work?

I am trying to make the whole function <div> name and URL passed parameters neutral so that I can just start tossing server data into uniquely named <div>'s. Does this make any sense?

If this works, email me your address so I can send you a check!

You rock!

Thanks

Jeff

var xmlHttp

function showCustomer(str,str2)

{

xmlHttp=GetXmlHttpObject();

if (xmlHttp==null)

{

alert ("Your browser does not support AJAX!");

return;

}

var url="http://onionsalad.com/search.fwx";

url=url+"?"+str;

url=url+"&sid="+Math.random();

xmlHttp.onreadystatechange=getStateChanged(str2);

xmlHttp.open("GET",url,true);

xmlHttp.send(null);

}

function getStateChanged(el)

{

if (typeof(el) === 'string')

{

el = document.getElementById(el);

}

return function() {

if (xmlHttp.readyState==4)

{

el.innerHTML = xmlHttp.responseText;

}

};

}
Copy linkTweet thisAlerts:
@toicontienAug 21.2007 — Try this bit of code out then:
var xmlHttp


/**
* @class window
*
* @function showCustomer
* Creates an AJAX request and fails silently if the browser doesn't
* support the necessary technology.
*
* @param str (string, required)
* The value to the sent over the network.
*
* @param el (variable, required)
* HTML tag Id, or node reference to the HTML tag whose innerHTML should
* be updated when the AJAX call is successful.
*
* @return boolean
* True is returned if the browser supports AJAX and the request was sent
* successfully, and false is returned if AJAX is not supported.
*/
function showCustomer(str, el) {
xmlHttp = GetXmlHttpObject();
if (xmlHttp == null) {
return false;
}
var url="http://onionsalad.com/search.fwx";
url = url+"?"+str;
url = url+"&amp;sid="+Math.random();
xmlHttp.onreadystatechange = getStateChanged(el);
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
return true;
}


/**
* @class window
*
* @function getStateChanged
* Returns the function object executed by the XMLHttpRequest object's
* onreadystatechange event.
*
* @param el (variable, required)
* Id or node reference to the HTML tag whose innerHTML should be updated.
*
* @return function
* The function object to be executed when the XMLHttpRequest object's
* ready state has changed.
*/
function getStateChanged(el) {
if (typeof(el) === 'string') {
el = document.getElementById(el);
}
return function() {
if (xmlHttp.readyState==4)
{
el.innerHTML = xmlHttp.responseText;
}
};
}
Copy linkTweet thisAlerts:
@jroehlauthorAug 21.2007 — Send your snailmail address to jroehl2 at yahoo.com and I will send you a check.
Copy linkTweet thisAlerts:
@toicontienAug 21.2007 — No need to. ? I haven't created anything unique. Enjoy and keep learning!
×

Success!

Help @jroehl 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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