473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DOM setAttribute onClick

69
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.

Expand|Select|Wrap|Line Numbers
  1. <input type="button" value="Add New Section" onclick="javascript:addSection()" />
Expand|Select|Wrap|Line Numbers
  1. function editSection(divId) 
  2. {
  3.      alert( "Edit Invoked" );
  4.      ....
  5. }
  6.  
  7. function addSection()
  8. {
  9.      alert( "Add section" );
  10.      ....
  11.      ....
  12.     var editButton = document.createElement( 'input' );
  13.     editButton.type = "button";
  14.     editButton.value = "Edit";
  15.     editButton.setAttribute('onClick', editSection('id'));
  16.     ....
  17.     ....
  18. }
  19.  
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
Sep 11 '07 #1
13 32468
pbmods
5,821 Expert 4TB
Heya, Sean.

Try encapsulating the function call in quotes:
Expand|Select|Wrap|Line Numbers
  1. editButton.setAttribute('onclick', "editSection('id');");
  2.  
You could alternatively do this:
Expand|Select|Wrap|Line Numbers
  1. editButton.setAttribute.onclick = function() { editSection('id'); };
  2.  
Sep 11 '07 #2
Sebarry
69
Works like magic!!!! You are an absolute star!!

Heya, Sean.

Try encapsulating the function call in quotes:
Expand|Select|Wrap|Line Numbers
  1. editButton.setAttribute('onclick', "editSection('id');");
  2.  
You could alternatively do this:
Expand|Select|Wrap|Line Numbers
  1. editButton.setAttribute.onclick = function() { editSection('id'); };
  2.  
Sep 11 '07 #3
Sebarry
69
Hi,

Your suggestion with the double quotes works great. The only problem I have now is that the id -
Expand|Select|Wrap|Line Numbers
  1. editSection('id')
- should be unique. I have a global variable - sectionCount - that I want to use to form a unique id -
Expand|Select|Wrap|Line Numbers
  1. 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
Sep 11 '07 #4
pbmods
5,821 Expert 4TB
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).

Expand|Select|Wrap|Line Numbers
  1. var newId = parseInt(id) + sectionCount;
  2. editButton.setAttribute('onclick', "editSection('" + newId + "');");
  3.  
Sep 11 '07 #5
Sebarry
69
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).

Expand|Select|Wrap|Line Numbers
  1. var newId = parseInt(id) + sectionCount;
  2. editButton.setAttribute('onclick', "editSection('" + newId + "');");
  3.  
Sep 11 '07 #6
pbmods
5,821 Expert 4TB
Heya, Sean.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sep 11 '07 #7
Sebarry
69
Sorry me again. Works great in good old Firefox but not IE. Do I have to use code such as:

Expand|Select|Wrap|Line Numbers
  1. var editButton = document.createElement( 'input' );
  2. editButton.type = "button";
  3. editButton.value = "Edit";
  4.  
  5. if( editButton.addEventListener ) {
  6.   editButton.addEventListener('click',editSection('test'),false);
  7. } else if( editButton.attachEvent ) {
  8.   editButton.attachEvent('onclick',editSection('test'));
  9. }
  10.  
Instead of
Expand|Select|Wrap|Line Numbers
  1. 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 :)
Sep 11 '07 #8
pbmods
5,821 Expert 4TB
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?
Sep 12 '07 #9
Sebarry
69
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:

Expand|Select|Wrap|Line Numbers
  1. var editButton = document.createElement( 'input' );
  2. editButton.type = "button";
  3. editButton.value = "Edit";
  4. editButton.setAttribute( 'onClick', "editSection('test');" );
  5.  
According to http://www.thescripts.com/forum/thread493656.html
Expand|Select|Wrap|Line Numbers
  1. setAtrribute
doesn't work in IE, but I've tried
Expand|Select|Wrap|Line Numbers
  1. 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?
Sep 12 '07 #10
acoder
16,027 Expert Mod 8TB
According to http://www.thescripts.com/forum/thread493656.html
Expand|Select|Wrap|Line Numbers
  1. setAtrribute
doesn't work in IE, but I've tried
Expand|Select|Wrap|Line Numbers
  1. editButton.onclick = "editSection('test');";
and that doesn't work.
Try:
Expand|Select|Wrap|Line Numbers
  1. editButton.onclick = function() { editSection('test'); };
Sep 12 '07 #11
Sebarry
69
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
Expand|Select|Wrap|Line Numbers
  1. editButton.onclick = function() { editSection('test' + count)}
but so it evaluates count now, not later. Earlier in the thread pbmods had provided
Expand|Select|Wrap|Line Numbers
  1. editButton.setAttribute('onClick', "editSection('test" + count + "');");
. I can't seem to do the same thing for
Expand|Select|Wrap|Line Numbers
  1. editButton.onclick = function()
.

Thanks,

Sean

Try:
Expand|Select|Wrap|Line Numbers
  1. editButton.onclick = function() { editSection('test'); };
Sep 12 '07 #12
acoder
16,027 Expert Mod 8TB
Try setting a variable to the value that you want to pass to the function and then pass that instead:
Expand|Select|Wrap|Line Numbers
  1. var testNum = 'test'+count;
  2. editButton.onclick = function() { editSection(testNum);}
Sep 13 '07 #13
pbmods
5,821 Expert 4TB
That won't work because the value of the count variable will be different by the time the function executes.

Instead, try using this:
Expand|Select|Wrap|Line Numbers
  1. editButton.onclick = function() { editSection(this); }
  2.  
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).
Sep 13 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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...
5
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...
21
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');...
5
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>...
11
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">...
4
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...
1
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,...
14
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.