Hi,
I have a form with an add button that when clicked should create a new textarea with edit and delete buttons and append it to the form. Both the edit and delete buttons need to respond to onClick events. Here's the code I have so far. - <input type="button" value="Add New Section" onclick="javascript:addSection()" />
-
function editSection(divId)
-
{
-
alert( "Edit Invoked" );
-
....
-
}
-
-
function addSection()
-
{
-
alert( "Add section" );
-
....
-
....
-
var editButton = document.createElement( 'input' );
-
editButton.type = "button";
-
editButton.value = "Edit";
-
editButton.setAttribute('onClick', editSection('id'));
-
....
-
....
-
}
-
The addSection function is invoked when the add button is clicked and this creates the edit button only, so far. However, when the onClick event is set for the edit button it invokes the editSection, which it should when the edit button is clicked, but it hasn't been clicked yet it's only be created.
Any ideas?
Thanks,
Sean
13 32468
Heya, Sean.
Try encapsulating the function call in quotes: -
editButton.setAttribute('onclick', "editSection('id');");
-
You could alternatively do this: -
editButton.setAttribute.onclick = function() { editSection('id'); };
-
Works like magic!!!! You are an absolute star!!
Heya, Sean.
Try encapsulating the function call in quotes: -
editButton.setAttribute('onclick', "editSection('id');");
-
You could alternatively do this: -
editButton.setAttribute.onclick = function() { editSection('id'); };
-
Hi,
Your suggestion with the double quotes works great. The only problem I have now is that the id -
- should be unique. I have a global variable - sectionCount - that I want to use to form a unique id - - editSection('id' + sectionCount)
- but this doesn't appear to form a string of id0, id1, id2 etc instead it forms the string whenever its used. Thus if I increment sectionCount the id is now id1 instead of id0 and id1 does not exist.
Any ideas?
Thanks,
Sean
Heya, Sean.
Try calculating the ID before adding it to the string. This will probably work better for you in the long run anyway, as then you are passing a constant value instead of relying on id not to change (long story, you can get a partial answer in this article). -
var newId = parseInt(id) + sectionCount;
-
editButton.setAttribute('onclick', "editSection('" + newId + "');");
-
Thanks again mate. That works perfectly.
Heya, Sean.
Try calculating the ID before adding it to the string. This will probably work better for you in the long run anyway, as then you are passing a constant value instead of relying on id not to change (long story, you can get a partial answer in this article). -
var newId = parseInt(id) + sectionCount;
-
editButton.setAttribute('onclick', "editSection('" + newId + "');");
-
Heya, Sean.
Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sorry me again. Works great in good old Firefox but not IE. Do I have to use code such as: -
var editButton = document.createElement( 'input' );
-
editButton.type = "button";
-
editButton.value = "Edit";
-
-
if( editButton.addEventListener ) {
-
editButton.addEventListener('click',editSection('test'),false);
-
} else if( editButton.attachEvent ) {
-
editButton.attachEvent('onclick',editSection('test'));
-
}
-
Instead of - editButton.setAttribute('onClick', "javascript:editSection('newsection" + sectionCount + "');");
. I get an object error in a try/catch block when I use the IE code but at least it actually calls the editSection() function. It again calls it before I want to so I guess the quotes are in order again but is the above IE right? and will it also work on FireFox?
Cheers,
Sean
Heya, Sean.
Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Heya, Sean.
Not to ask an obvious question... but you *are* setting the ID of each new element that you are creating to match up with what you're passing to the onclick handler, yes?
Hi,
No unfortunately I'm not. In fact at the moment I don't think it matters because I'm setting the onclick event to just the name of the function and the string test, just to try and get it to invoke the function.
What I have been using is: -
var editButton = document.createElement( 'input' );
-
editButton.type = "button";
-
editButton.value = "Edit";
-
editButton.setAttribute( 'onClick', "editSection('test');" );
-
According to http://www.thescripts.com/forum/thread493656.html
doesn't work in IE, but I've tried - editButton.onclick = "editSection('test');";
and that doesn't work.
Hope you can help.
Sean
Heya, Sean.
Not to ask an obvious question... but you *are* setting the ID of each new element that you are creating to match up with what you're passing to the onclick handler, yes?
According to http://www.thescripts.com/forum/thread493656.html
doesn't work in IE, but I've tried - editButton.onclick = "editSection('test');";
and that doesn't work.
Try: - editButton.onclick = function() { editSection('test'); };
Hi,
That works great and fixes it on IE, but what I need to do now is pass a string concatenated with a number i.e - editButton.onclick = function() { editSection('test' + count)}
but so it evaluates count now, not later. Earlier in the thread pbmods had provided - editButton.setAttribute('onClick', "editSection('test" + count + "');");
. I can't seem to do the same thing for - editButton.onclick = function()
.
Thanks,
Sean
Try: - editButton.onclick = function() { editSection('test'); };
Try setting a variable to the value that you want to pass to the function and then pass that instead: - var testNum = 'test'+count;
-
editButton.onclick = function() { editSection(testNum);}
That won't work because the value of the count variable will be different by the time the function executes.
Instead, try using this: -
editButton.onclick = function() { editSection(this); }
-
When editButton gets clicked, editSection() will get called with this as the first parameter, and in the context of the event, this will be a reference to the element that was clicked (remember: the function won't be evaluated until editButton's click event fires).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tim Johnson |
last post by:
Hello All:
Using javascript to dynamically add row elements to a table.
as in .....
row.setAttribute("bgcolor",rowColor);
// or
cell.setAttribute("bgcolor",rowColor);
Using firefox or...
|
by: johnsuth |
last post by:
I want to produce a trivial demonstration of dynamic modification.
I thought that pressing a button might change its color.
I studied O'Reillys books and successfully created the button with a...
|
by: James Black |
last post by:
I am curious if there is a benefit to set attributes directly, in my
javascript, or to use setAttribute.
For example, I have this:
var input = document.createElementNS(xhtmlNS, 'input');...
|
by: gkelly |
last post by:
Can someone explain what I am doing wrong, or why this will not work?
I've tested this in IE6, Firefox 1.5 and Mozilla 1.7, all with the same
result.
Take for example this code:
<html>...
|
by: jesdynf |
last post by:
I'm having trouble applying a stylesheet to content I'm generating
after the fact.
Here's the sample code:
<html>
<head>
<title>CSS/DOM Problem Example</title>
<style type="text/css">...
|
by: ICPooreMan |
last post by:
I've got some code which works in firefox that's giving me fits in
IE7 (maybe other versions too I haven't tested it). What I want to do
is get the oncontextmenu attribute of something, change the...
|
by: theS70RM |
last post by:
yea i know there is millions of stuff about this on the net, and this isnt really a question but just wanted to see if people had picked up anything similar.
This is only for internet explorer,...
|
by: Sri02 |
last post by:
All,
I was trying to add an onclick event using setAttribute to a <tdfrom
javascript. Apparently the code doesnot seem to work in FF3 but works
pretty well in IE7.
Here is the snippet for ur...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
| |