Add onClick functionality using Javascript

K

Kevin

Howdy,

How can I reference a function and include a parameter from a
javascript?

I need to include two inputs, xmlDoc and location, for my saveFile
function. Without those inputs, my saveFile won’t work.

var myButton = document.getElementsByName(“saveButton”)[0];
myButton.onclick = saveFile(xmlDoc,location);

Note that the code above does not work.

Thank you,
Kevin
 
M

Matt Kruse

myButton.onclick = saveFile(xmlDoc,location);

This will execute saveFile with those two arguments, and assign the
result to onclick.

You want:
myButton.onclick = function() { saveFile(xmlDoc,location); };

Matt Kruse
 
K

Kevin

You want:
myButton.onclick = function() { saveFile(xmlDoc,location); };

Matt Kruse

Hi Matt,

Thanks for your reply. When I call this javascript file (see snippet
below), it doesn't call "myButton.onclick = ..." because I don't
receive the alert, "saved changes to location."

function saveXML(xmlDoc, location)
{
var myButton = document.getElementsByName("saveButton")[0];
myButton.onclick = function() { saveFile(xmlDoc,location); };
}

function saveFile(xmlDoc, location)
{
xmlDoc.save(location);
alert("Saved changes to " + location);
}
 
N

nick

When I call this javascript file (see snippet
below), it doesn't call "myButton.onclick = ..." because I don't
receive the alert, "saved changes to location."

You should post an example with html included, otherwise it's
impossible to tell what's going on. If I had to guess I'd say you're
trying to assign the onclick attribute to an element that doesn't
exist yet (i.e. dom not ready when script runs).

First, though, check for error messages, and check that
document.getElementsByName("saveButton") actually has a length.
 
K

Kevin

Two functions:

function drawHotLinkButton(name) // onBodyLoad calls "paint the
screen," which calls drawHotLinkButton(name) to draw a button
// on the screen that opens a new
webpage, webpage.html?xml=something.xml, when the button's clicked.
{
document.write("<input name=\"" + name + "\" type=\"button\" \/
}

function populateHotLinkButtons(xmlDoc) // this function adds the
onclick functionality to the button.
// note that I didn't set a
value in the drawHotLinkButton() above. I'm trying
// to add the button's value
in this function so that I can set it based on the XML DOM
{
var XMLnode = xmlDoc.getElementsByTagName("Name")
[0].childNodes[0];
var HTMLnode = document.getElementsByName("Name")[0];
HTMLnode.value = XMLnode.nodeValue;

HTMLnode.onclick = function() { window.location="webpage.html?xml="
+ value + ".xml" };
}
 
S

SAM

Le 6/7/10 6:16 AM, Kevin a écrit :
Two functions:

function drawHotLinkButton(name) // onBodyLoad calls "paint the
screen," which calls drawHotLinkButton(name) to draw a button
// on the screen that opens a new
webpage, webpage.html?xml=something.xml, when the button's clicked.
{
document.write("<input name=\"" + name + "\" type=\"button\" \/
}

function drawHotLinkButton(aName) {
var b = document.createElement('BUTTON');
b.name = aName;
b.innerHTML = 'save';
b.onclick = upToDate;
document.getElementById('somewhere').appendChild(b);
}

function upToDate() {
if(xmlDoc && xmlDoc.getElementsByName(this.name)[0])
{
this.value = xmlDoc.getElementsByName(this.name)[0].nodeValue;
alert(document.getElementsByName(this.name)[0].value); // verif
}
else alert('KK prout prout');
}
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top