473,378 Members | 1,688 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,378 software developers and data experts.

js generated input button not working

8
Hmm this forum really doesn't give you long enough to type in your question before logging you out.. well here goes my second attempt:

I'm trying to teach myself javascript with dom scripting and am attempting to write an application as I learn. It's been going fine but I've ran into a problem which is driving me crazy.

I'll start off by explaining what I'm trying to achieve... I'm simply trying to insert an input button into a table at a specific place which calls a function when clicked. For examples sake I'm trying to place it into row 3, column 4 of the table.

So I started off by creating an button that called the javascript to insert that button. This works fine. In the script I first define the new element like so :

Expand|Select|Wrap|Line Numbers
  1. var newDelete = document.createElement('input');
  2. newDelete.setAttribute('type', 'button');
  3. newDelete.setAttribute('value', 'delete');
  4. newDelete.setAttribute('onclick', 'test();');
I then insert it into the correct place in the table :

Expand|Select|Wrap|Line Numbers
  1. document.getElementsByTagName('table')[0].firstChild.childNodes[2].childNodes[3].appendChild(newDelete);
I realise there are probably better ways to find the correct 'td' in the table but I'm a newbie.

At first it appears to run fine... I click the static button, and the new button appears in the table at the correct position. You can even click the button!!! However, clicking the button does not call the test() function as expected. Looking at the generated html the button looks ok :

Expand|Select|Wrap|Line Numbers
  1. <INPUT onclick=test(); type=button value=delete>
To test whether it was a problem with the html generated I copied the button html into a separate html page and opened it. The button appeared... clicking it called the test() function!

So even though these two buttons are effectively the same... clicking the dynamically generated button does not call the test() function... while clicking the static one does...

Any help would be greatly appreciated!

Ben.
Oct 4 '07 #1
18 2575
gits
5,390 Expert Mod 4TB
hi ...

try to not use the setAttribute-method for it ... use something like that:

Expand|Select|Wrap|Line Numbers
  1. var click_handler = function(this) {
  2.     alert(this.nodeName);
  3. };
  4.  
  5. newDelete.onclick = click_handler;
kind regards
Oct 4 '07 #2
mrhoo
428 256MB
You were pretty close.

Expand|Select|Wrap|Line Numbers
  1. var B= document.createElement('input')
  2. B.type= 'button';
  3. B.value= 'Delete';
  4. B.onclick= test;  // reference the function, don't call() it  here
.


Finding a particular cell in a table is a common and tiresome task, you might want a function just for that.
Expand|Select|Wrap|Line Numbers
  1. function getCell(t,r,c){
  2.     t= document.getElementsByTagName('table')[t-1];
  3.     r= t.getElementsByTagName('tr')[r-1];
  4.     var tem,i= 0;
  5.     while(r.childNodes[i] && c>0){
  6.         tem= r.childNodes[i];
  7.         if(/^(td|th)$/i.test(tem.nodeName)){
  8.             if(--c== 0) return tem;
  9.         }
  10.         ++i;
  11.     }
  12.     return null;
  13. }
To add the button defined above to the first table, third row, fourth cell:

getCell(1,3,4).appendChild(B);
Oct 5 '07 #3
dmjpro
2,476 2GB
Hmm this forum really doesn't give you long enough to type in your question before logging you out.. well here goes my second attempt:

I'm trying to teach myself javascript with dom scripting and am attempting to write an application as I learn. It's been going fine but I've ran into a problem which is driving me crazy.

I'll start off by explaining what I'm trying to achieve... I'm simply trying to insert an input button into a table at a specific place which calls a function when clicked. For examples sake I'm trying to place it into row 3, column 4 of the table.

So I started off by creating an button that called the javascript to insert that button. This works fine. In the script I first define the new element like so :

Expand|Select|Wrap|Line Numbers
  1. var newDelete = document.createElement('input');
  2. newDelete.setAttribute('type', 'button');
  3. newDelete.setAttribute('value', 'delete');
  4. newDelete.setAttribute('onclick', 'test();');
I then insert it into the correct place in the table :

Expand|Select|Wrap|Line Numbers
  1. document.getElementsByTagName('table')[0].firstChild.childNodes[2].childNodes[3].appendChild(newDelete);
I realise there are probably better ways to find the correct 'td' in the table but I'm a newbie.

At first it appears to run fine... I click the static button, and the new button appears in the table at the correct position. You can even click the button!!! However, clicking the button does not call the test() function as expected. Looking at the generated html the button looks ok :

Expand|Select|Wrap|Line Numbers
  1. <INPUT onclick=test(); type=button value=delete>
To test whether it was a problem with the html generated I copied the button html into a separate html page and opened it. The button appeared... clicking it called the test() function!

So even though these two buttons are effectively the same... clicking the dynamically generated button does not call the test() function... while clicking the static one does...

Any help would be greatly appreciated!

Ben.

Have a look at this ...............
Expand|Select|Wrap|Line Numbers
  1. <td id = "some_id"></td>
  2.  
Expand|Select|Wrap|Line Numbers
  1. function addChild()
  2. {
  3.  var ref = document.createElement("input");
  4.  ref.setAttribute("type","text");
  5.  .
  6.  .
  7.  typeof window.attachEventListener ? ref.attachEventListener("click",test,true) : ref.addEventListener("onclick",test);
  8.  document.getElementById('some_id').appendChild(ref);
  9. }
  10. function test()
  11. {
  12. //some code
  13. }
  14.  
Debasis Jana
Oct 5 '07 #4
ajos
283 100+

Expand|Select|Wrap|Line Numbers
  1. <INPUT onclick=test(); type=button value=delete>
Ben.
hey ben,
i doubt the way you ve declared the above line,though im not sure abt wat you are doing with this, i think you shud do something like this-->
Expand|Select|Wrap|Line Numbers
  1. <INPUT type="button" value="delete" onclick="test()">
  2.  
just give it a try:)
regards,
ajos
Oct 5 '07 #5
bning
8
Thanks for all your quick replies! Eliminating the setAttributes and referencing the function rather than calling it (thanks mrhoo!) made the button work finally. And that cell finding function will no doubt save me a LOT of time.

Thanks again everyone!
Oct 5 '07 #6
bning
8
Hmmm another problem hehe...

this example required no values be be passed to the test function ... however the actual application needs the name of the input button to be passed to the test function... now as adding (this.name) to the onclick attribute causes loss of function to the button... how might I achieve this?

Ben.
Oct 5 '07 #7
dmjpro
2,476 2GB
Hmmm another problem hehe...

this example required no values be be passed to the test function ... however the actual application needs the name of the input button to be passed to the test function... now as adding (this.name) to the onclick attribute causes loss of function to the button... how might I achieve this?

Ben.
Post your Code and tell what you are not achieving.

Debasis Jana
Oct 5 '07 #8
bning
8
The real application lets you enter values into a form then click a button which then adds a row to a table containing these values. A button is the added to the end of the row which when clicked deletes this row. Now when I create the row I will assign the postion of the row in the table as its name attribute, and ideally when this delete button is clicked it will pass its name to a function so the function knows which row to delete.

Expand|Select|Wrap|Line Numbers
  1. var newDelete = document.createElement('input');
  2. newDelete.type = 'button';    
  3. newDelete.name = 'row2';
  4. newDelete.value = 'delete';
  5. newDelete.onclick = test;
  6. document.getElementsByTagName('table')[0].firstChild.childNodes[emptyRowNum].childNodes[5].appendChild(newDelete);
The above code creates the delete button. As discussed above the dynamically created button will not call the test function if the onclick attribute is assigned as 'test()' but will work when its assigned as 'test'. However, this will not allow me to pass the name of the input button to the test function by using 'test(this.name)'.

Any help would be greatly appreciated .

Ben.
Oct 5 '07 #9
dmjpro
2,476 2GB
The real application lets you enter values into a form then click a button which then adds a row to a table containing these values. A button is the added to the end of the row which when clicked deletes this row. Now when I create the row I will assign the postion of the row in the table as its name attribute, and ideally when this delete button is clicked it will pass its name to a function so the function knows which row to delete.

Expand|Select|Wrap|Line Numbers
  1. var newDelete = document.createElement('input');
  2. newDelete.type = 'button';    
  3. newDelete.name = 'row2';
  4. newDelete.value = 'delete';
  5. newDelete.onclick = test;
  6. document.getElementsByTagName('table')[0].firstChild.childNodes[emptyRowNum].childNodes[5].appendChild(newDelete);
The above code creates the delete button. As discussed above the dynamically created button will not call the test function if the onclick attribute is assigned as 'test()' but will work when its assigned as 'test'. However, this will not allow me to pass the name of the input button to the test function by using 'test(this.name)'.

Any help would be greatly appreciated .

Ben.
Is that necessary to pass that?

Expand|Select|Wrap|Line Numbers
  1. .
  2. .
  3. button_ref.setAttribute("id","some_id");
  4. .
  5. .
  6. function test()
  7. {
  8.  alert(document.getElementById("some_id").getAttribute("name"));
  9. }
  10.  
Debasis Jana
Oct 5 '07 #10
bning
8
There would be one delete button for each row in the table. Clicking the button on a row should delete that row and no others. Even assigning a unqiue id to each button would not allow the test function to know which button had called it unless I could pass it a variable? Although I'm probably missing something.
Oct 5 '07 #11
gits
5,390 Expert Mod 4TB
hi ...

you may use the event-obj for this ... here i show you the standards-compliant method:

[HTML]
<script type="text/javascript">
function add_click() {
var newDelete = document.getElementById('test');

var click_handler = function(e) {
var obj = e.target;

alert(obj.name);
};

newDelete.onclick = click_handler;
}
</script>

<body onload="add_click();">
<input type="button" id="test" name="test_button" value="click_me"/>
</body>
[/HTML]

note: IE has no e.target but srcElement ... so it has to be adapted for cross-browser-capabilities

kind regards
Oct 5 '07 #12
dmjpro
2,476 2GB
There would be one delete button for each row in the table. Clicking the button on a row should delete that row and no others. Even assigning a unqiue id to each button would not allow the test function to know which button had called it unless I could pass it a variable? Although I'm probably missing something.
Sorry you can do it another way ........ :-)
Without having my code, i said earlier.
Actually it is possible in IE....I tested that in Mozilla i failed.

Expand|Select|Wrap|Line Numbers
  1. function test()
  2. {
  3.  alert(event.srcElement.name);
  4. }
  5.  
Debasis Jana
Oct 5 '07 #13
gits
5,390 Expert Mod 4TB
:) now ... simply combine the last two posts :))

kind regards
Oct 5 '07 #14
dmjpro
2,476 2GB
hi ...

you may use the event-obj for this ... here i show you the standards-compliant method:

[HTML]
<script type="text/javascript">
function add_click() {
var newDelete = document.getElementById('test');

var click_handler = function(e) {
var obj = e.target;

alert(obj.name);
};

newDelete.onclick = click_handler;
}
</script>

<body onload="add_click();">
<input type="button" id="test" name="test_button" value="click_me"/>
</body>
[/HTML]

note: IE has no e.target but srcElement ... so it has to be adapted for cross-browser-capabilities

kind regards
Does browser send the event object when an event fired up :-)

Debasis Jana
Oct 5 '07 #15
gits
5,390 Expert Mod 4TB
Does browser send the event object when an event fired up :-)

Debasis Jana
in case this is a question the answer is yes ... except IE which has a global event-object.

kind regards
Oct 5 '07 #16
dmjpro
2,476 2GB
in case this is a question the answer is yes ... except IE which has a global event-object.

kind regards
Oh right now I can understand......
Actually why I was unable to use "event" without passing it in Mozilla.
Really you are very impressive man :-)

Debasis Jana
Oct 5 '07 #17
bning
8
Thanks guys! Working perfectly now :)
Oct 5 '07 #18
gits
5,390 Expert Mod 4TB
hi ...

glad to hear that :) ... post back to the forum anytime you have more questions ...

kind regards
Oct 5 '07 #19

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

Similar topics

3
by: KathyB | last post by:
Hi, I'm trying to find a way to validate input text boxes where I don't know the names until the page is rendered. I've got 2 validate functions that fire with the onsubmit button of a "mini" form...
1
by: MikeC | last post by:
For ASP.Net/C Sharp I have an XML file and an associated XSL file. I am using the System.Web.UI.Controls.Xml control to render the resulting HTML. This seems to work fine with one slight...
0
by: Wysiwyg | last post by:
Hi, I just thought I'd post this since I didn't see anyone else doing it this way. I wanted to be able to force the enter key to submit the form without manually changing the html form, In my...
4
by: Jack | last post by:
THE FOLLOWING IS A PART OF CODE FROM A ASP PAGE <% sql01 = "SELECT COUNT(*) AS reccount FROM Equipmenttbl " sql01 = sql01 & "WHERE Equipmenttbl.GrantID = " & GrantID 'Response.Write sql01 &...
2
by: ShaneFowlkes | last post by:
I created a couple of quick test pages and to my surprise, these do not work either. When I click "Go Back" on test2.aspx, nothing happens. Ideas?? Surely I must be missing something obvious........
5
by: mk | last post by:
Greetings all - im new to this newsgroup so pls dont flame me :p I need some help! Please view the html below in a browser. Or goto this url -http://firebrain.co.uk/java-problem.htm (Assuming...
1
verbatim
by: verbatim | last post by:
with the following page, the dynamically generated fields are not recognized when i try to submit the form, or add more elements. when i hit my submit button, the address bar has only x1 - x5 in...
7
by: Srikanth Ram | last post by:
Hi, I'm creating a PHP application. In this a dynamic table with the fields in the database is generated in a page. I have placed a checkbox in each row of the table to approve/disapprove...
5
by: dwmartin18 | last post by:
Hello everyone. I have quite the puzzling problem with a script I have been working on lately. I have created a function that can be called to create a new html element (e.g. input, select, div,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.