Jump to content

[SOLVED] dynamically attach function to a submit button?


sayedsohail

Recommended Posts

Hi everyone,

 

I wish to attach a function to a submit button when users click New button.  I can assign it directly, but my requirement is to attach on the fly.

 

Here is my form

 

<form id='test'>
<input type='name' value=''>
<input type='email' value=''>
<input type='button' value='New' onclick='assigned function to submit button'>. 
<input type='submit' value='Update'>
</form>

// here is the simple function written in javascript which needs to be attached to the submit button,

<script>

function insertdata()

{.....}

</script>

 

Can someone suggest how to attach function to your submit button when users clicks the new button.  (Offcourse i can assign the function to the submit button directly, but my requirement is attached on the fly).

 

Thanks

Sohail

Link to comment
Share on other sites

could you have the submit button as

<input type='submit' value='Update' onclick="insertdata('new')" />

 

Then on the 'New' button have something like:

<input type='button' name="new" value='New' id="new" onclick='set_value('new')'>.

 

The set_value command could change the value of the 'new' button, by getting the layer from the id specified.

Then the submit button collects the value of the new button to insert it.

 

I'm aware that this is a poor example, but I'm only just starting with javascript and it's just an idea to start with.

Hope it helps

Link to comment
Share on other sites

well million thanks, this is so far i tried still no SUCCESS:

 

<form id='test'>
<input type='name' value=''>
<input type='email' value=''>
<input type='button' value='New' onclick='addfunction();'>. 
<input type='submit' value='Update'>
</form>

 

// here is the simple function written in javascript which LINKS the function insertnotes() to the submit button,

<script>
function addfunction()
{

if (window.attachEvent) {
document.getElementById("submit").attachEvent("onclick",insertnotes);
}
else if (window.addEventListener) {
document.getElementById("submit").addEventListener("onclick",insertnotes,true);
}
}

/// This function needs to be linked to the submit button.
function insertnotes()
{
....
}
</script>

Link to comment
Share on other sites

i just change button id='task' and addEventListener with click instead of onclick, it works fine.

 

document.getElementById("task").addEventListener("click",updatenotes,false);

 

Unfortunately, if i try to add additional addEventListener to the smae button, results multiple functions attached to the same button.

 

How could i attached only the recent addeventlistener to the button.

Link to comment
Share on other sites

okay. I'm not sure about quite how to use it, but I think the idea is something like this:

function remove_function(id, fun){
if (window.detachEvent) {
	document.getElementById(id).detachEvent('onclick', fun);
}else if(window.removeEventListener){
	document.getElementById(id).removeEventListener('click',fun,false);
}
}

 

just call it before adding a function:

javascript:

<script>
function addfunction(id, fun)
{
remove_function(id, fun);

if (window.attachEvent) {
document.getElementById(id).attachEvent("onclick",insertnotes);
}
else if (window.addEventListener) {
document.getElementById(id).addEventListener("onclick",insertnotes,true);
}
}

/// This function needs to be linked to the submit button.
function insertnotes()
{
....
}


function remove_function(id, fun){
if (window.detachEvent) {
	document.getElementById(id).detachEvent('onclick', fun);
}else if(window.removeEventListener){
	document.getElementById(id).removeEventListener('click',fun,false);
}
}
</script>

 

html:

<form id='test'>
<input type='name' value=''>
<input type='email' value=''>
<input type='button' value='New' onclick='addfunction('task', 'addnotes');'>. 
<input type='submit' value='Update'>
</form>

obviously in the onclick change 'task' to the button id (I'm presuming task), and 'addnotes' to the function name you want to remove.

 

I hope that both helps and works!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.