473,666 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding javasript property to a cell in dynamically added rows.

10 New Member
Hi.. i am new to javascripts,

i am using a addRow scipt to add rows to a table dynamically.

one of the cells(say cell2) in a Nth row needs to be hyperlinked to another script.

for example:
Expand|Select|Wrap|Line Numbers
  1. function addRow() // the add row script
  2. {
  3.     var tbl=document.getElementById("table");
  4.     var numRows=tbl.rows.length;
  5.     var newRow=tbl.insertRow(numRows);
  6.  
  7.      //IDs
  8.     var secondCell=newRow.insertCell(2);
  9.     var box2=document.createElement('TD');
  10.     //textbox
  11.     var txtInp2=document.createElement('input');
  12.     txtInp2.name="IDs";
  13.     txtInp2.type='text';
  14.     txtInp2.value="";
  15.     //image
  16.     var link2=document.createElement('<a href="#"onClick="javascript:Popup(this)">');
  17.  
  18. var image2=document.createElement('<IMG align=middle border=0 src='find.gif'>');
  19.     link2.appendChild(image2);
  20.     box2.appendChild(txtInp2);
  21.     box2.appendChild(link2);
  22.     secondCell.appendChild(box2);
  23.  
  24. // i am appending image to link , txtInp2 and link to the box, and box2 to //secondCell.
  25. }
  26. function Popup(obj)
  27.     {
  28.         currentRowIndex = obj.parentElement.parentElement.rowIndex;
  29.         obj = document.form.IDs;
  30.         if(obj.length != null)
  31.         {
  32.             window.Id=document.form.IDs(currentRowIndex-1);
  33.         }
  34.         else
  35.         {
  36.             window.Id=obj;
  37.         }
  38.  
  39.         var urlString = <something>;
  40.         image=window.open(urlString,"width=480,height=230,status");
  41.         image.focus();
  42.     }
  43.  
My problem is, if the table is already defined(if href to PopUp() specified in first row of table,declared in HTML, then link to PopUp(obj) is working , but if i add rows to the table dynamically using addRow(), the links to PopUp() in the new rows added are not working,
it gives a error msg telling either "object expected" or "parentElement. parentElement" ,or "length is null or not an object"

I dont know how to specify the 'this' in
var link2=document. createElement(' <a href="#"onClick ="javascript:Po pup(this)">');

PLZ help...!
Aug 23 '07 #1
18 2770
epots9
1,351 Recognized Expert Top Contributor
Hi.. i am new to javascripts,

i am using a addRow scipt to add rows to a table dynamically.

one of the cells(say cell2) in a Nth row needs to be hyperlinked to another script.

for example:

function addRow() // the add row script
{
var tbl=document.ge tElementById("t able");
var numRows=tbl.row s.length;
var newRow=tbl.inse rtRow(numRows);

//IDs
var secondCell=newR ow.insertCell(2 );
var box2=document.c reateElement('T D');
//textbox
var txtInp2=documen t.createElement ('input');
txtInp2.name="I Ds";
txtInp2.type='t ext';
txtInp2.value=" ";
//image
var link2=document. createElement(' <a href="#"onClick ="javascript:Po pup(this)">');

var image2=document .createElement( '<IMG align=middle border=0 src='find.gif'> ');
link2.appendChi ld(image2);
box2.appendChil d(txtInp2);
box2.appendChil d(link2);
secondCell.appe ndChild(box2);

// i am appending image to link , txtInp2 and link to the box, and box2 to //secondCell.
}
function Popup(obj)
{
currentRowIndex = obj.parentEleme nt.parentElemen t.rowIndex;
obj = document.form.I Ds;
if(obj.length != null)
{
window.Id=docum ent.form.IDs(cu rrentRowIndex-1);
}
else
{
window.Id=obj;
}

var urlString = <something>;
image=window.op en(urlString,"w idth=480,height =230,status");
image.focus();
}

My problem is, if the table is already defined(if href to PopUp() specified in first row of table,declared in HTML, then link to PopUp(obj) is working , but if i add rows to the table dynamically using addRow(), the links to PopUp() in the new rows added are not working,
it gives a error msg telling either "object expected" or "parentElement. parentElement" ,or "length is null or not an object"

I dont know how to specify the 'this' in
var link2=document. createElement(' <a href="#"onClick ="javascript:Po pup(this)">');

PLZ help...!
try this:
Expand|Select|Wrap|Line Numbers
  1. var link2=document.createElement('a');
  2. link2.href = "#";
  3. link2.onclick = function(){Popup(this);};
  4.  
try that out and tell us how it goes,
good luck
Aug 23 '07 #2
pbmods
5,821 Recognized Expert Expert
Heya, Sandeep. Welcome to TSDN!

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Aug 23 '07 #3
sandeepdesai
10 New Member
sorry..I tried what u suggested, but it is not working.

My full code with form element goes like this,might be there is some error in these too.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script>
  3. function(addRow)
  4. {
  5.     var tbl=document.getElementById("table");
  6.     var newRow=tbl.insertRow(tbl.rows.length);
  7.  
  8.     <note>0th and 1st cell added,which are textboxes<note>
  9.  
  10.     var secondCell=newRow.insertCell(2);
  11.     var txtInp2=document.createElement('input');
  12.     txtInp2.name="ids"; //setting name to newly added cell.
  13.     txtInp2.type='text';
  14.     txtInp2.value="";
  15.  
  16.      var link2=document.createElement('a');
  17.      link2.href = "#";
  18.      link2.onclick = function(){Popup(this);};
  19.      link2.value=click;
  20.      secondCell.appendChild(txtInp2);
  21.      secondCell.appendChild(link2);
  22.     tbl.appendChild(newRow);
  23. }
  24. function PopUp(obj)
  25. {
  26. currentRowIndex = obj.parentElement.parentElement.rowIndex;
  27.  
  28.         obj = document.form.ids;
  29.         if(obj.length != null)// <NOTE> im getting an error here saying obj.length is null or not an object.<NOTE>
  30.         {
  31.             window.id=document.form.ids(currentRowIndex-1);
  32.         }
  33.         else
  34.         {
  35.             window.id=obj;
  36.         }
  37.  
  38.         urlString = "\findId.jsp";
  39.         newWindow=window.open(urlString,'find',"width=480,height=230");
  40.         newWindow.focus();
  41. }
  42. </script>
  43.  
and the FORM element goes like this..
Expand|Select|Wrap|Line Numbers
  1. <form name=form>
  2.   <table id=table>
  3.    <tr>
  4.     <td><input type=textbox name=text1></td>
  5.     <td><input type=textbox name=text2></td>
  6.     <td><a href="#" onclick="PopUp(this)" value=click><input type=textbox name=ids></a></td>
  7.   </tr>
  8.  </table>
  9.  
  10. <input type=button onclick="addRow()">
  11. </form>
  12.  
I am getting the error when i click the link in the dynamically added rows.for the first row the pop up is working.Is there any error in the method i have used for naming the 2nd textbox??
i tried txtInp2.setAttr ibute('name','i ds')also but it did not work.
using alert(ids.lengt h)in the PopUp function also gives the same error.

again ,alert(document .form.ids.lengt h)gives 1.

THANKS..!!!!
sandeep.
Aug 28 '07 #4
dmjpro
2,476 Top Contributor
You better to use parentNode instead of parentElement.
parentElement is only supported by IE, but parentNode supported by all browser.
Try it I think it will work fine.
Gud luck,

Kind regards,
Dmjpro.
Aug 28 '07 #5
sandeepdesai
10 New Member
You better to use parentNode instead of parentElement.
parentElement is only supported by IE, but parentNode supported by all browser.
Try it I think it will work fine.
Gud luck,

Kind regards,
Dmjpro.
Thanks.. I used parentNode instead of parentElement., but i am getting the same error..
I think i am not able to name the newly added text boxes in the rows generated by addRow() script.....

alert(document. form.ids.length ) is giving me 1,(first row already present in the form before adding rows..)
even after i add some extra rows by add row script.....i think there is some error in the method i have used for adding name attribute for 2nd textbox... I am not able to access them via their names.
i have used
Expand|Select|Wrap|Line Numbers
  1. function addRow()
  2. {
  3.       var secondCell=newRow.insertCell(2);
  4.     var txtInp2=document.createElement('input');
  5.     txtInp2.name="ids"; // is there any error in this?????????
  6.     txtInp2.type='text';
  7.     txtInp2.value="";
  8.  
  9. }
  10.  
plzzzzzz help....
thanks and regards..
sandeep.
Aug 30 '07 #6
dmjpro
2,476 Top Contributor
Thanks.. I used parentNode instead of parentElement., but i am getting the same error..
I think i am not able to name the newly added text boxes in the rows generated by addRow() script.....

alert(document. form.ids.length ) is giving me 1,(first row already present in the form before adding rows..)
even after i add some extra rows by add row script.....i think there is some error in the method i have used for adding name attribute for 2nd textbox... I am not able to access them via their names.
i have used
Expand|Select|Wrap|Line Numbers
  1. function addRow()
  2. {
  3.       var secondCell=newRow.insertCell(2);
  4.     var txtInp2=document.createElement('input');
  5.     txtInp2.name="ids"; // is there any error in this?????????
  6.     txtInp2.type='text';
  7.     txtInp2.value="";
  8.  
  9. }
  10.  
plzzzzzz help....
thanks and regards..
sandeep.
You new row Created or not?
Please let me know first.

Kind regards,
Dmjpro.
Aug 30 '07 #7
sandeepdesai
10 New Member
ya... my new row is getting created without any error.......
only the link for pop up in the newly added rows is not working.... :(
Aug 30 '07 #8
dmjpro
2,476 Top Contributor
ya... my new row is getting created without any error.......
only the link for pop up in the newly added rows is not working.... :(
I think these two lines reporting the error.

Expand|Select|Wrap|Line Numbers
  1. var link2=document.createElement('<a href="#"onClick="javascript:Popup(this)">');
  2. var image2=document.createElement('<IMG align=middle border=0 src='find.gif'>');
  3. //These two line are wrong.
  4.  
Expand|Select|Wrap|Line Numbers
  1. var link2 =document.createElement('a');
  2. link2.setAttribute('href','javascript:void(0)');
  3. link2.setAttribute('onclik','javascript:Popup(this)');
  4. var image2=document.createElement('img');
  5. image2.setAttribute('align','middle');
  6. image2.setAttribute('border','0');
  7. image2.setAttribute('src','find.gif');
  8. //Replace those two line with these lines then see what happens.
  9.  
Best of luck with your try.

Kind regards,
Dmjpro.
Aug 30 '07 #9
sandeepdesai
10 New Member
Thanks for replying.
i tried what u suggested. But again i am getting the same error.
I there is a problem where i am assigning names to the text box (txtInp2.setAtt ribute('name',' ids');

I actually copied the two scripts from somewhere, and i think it works this way.
Expand|Select|Wrap|Line Numbers
  1. 1.obj = document.form.ids;
  2. // 1->This should give me list of all all form elements with name 'ids';
  3.  
  4. 2.if(obj.length != null)
  5.    window.id=document.form.ids(currentRowIndex-1);
  6.  else
  7.   window.id=obj;
  8. //2-> this should set the value ' window.id' (got from jsp) to the textbox adjacent to the image which is clicked,since there are many textboxes with name "ids"
  9.  
  10. 3.urlString = "find.jsp'    newWindow=window.open(urlString,"lookup","width=480,height=230);
  11.     newWindow.focus();
  12. //3-> this should open the find.jsp in a new window and focus on it from which user can select an id by clicking on a already existing id(jsp fetches list of existing ids when parameter 'lookup' is passed to it.)
  13.  
I am getting a problem in line2, and the error is
"obj.length is null or not an object",
which means the script is not able to access the newly added textbox with name "ids".....

document.form.i ds shouldgive me all the form elements with name "ids",
but i am not able to get the newly added elements(textbo xes with name "ids") that are added to the form using addRow().

Is there any other method to get access to the form elements by name ?

thanks...!
regards,
sandeep.
Aug 30 '07 #10

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

Similar topics

4
5476
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
3
4872
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
2
1964
by: Praveen Balanagendra via .NET 247 | last post by:
here is the source code private void AddRow() { TableCell tc = new TableCell(); tc.Controls.Add(new LiteralControl("NewRow")); DataGridItem di = new DataGridItem(DataGrid1.Items.Count+1,DataGrid1.Items.Count+1,ListItemType.Item); di.Cells.Add(tc); di.Cells.Add(tc1);
4
1596
by: Sandeep | last post by:
Hi I am doing one thing in my website ,actually i want to add controls dynamically to a web form and want to access the elements but when i postback the form,and access that control, it gives error Object reference not set to an instance of an object. ************************************************************************ ************************************************************************** Public Class index2 Inherits...
0
1871
by: Sileesh | last post by:
Hi I have html table and a Button in an Aspx page. I am adding one row with some textboxes to Html table each time i click on the Button thru Javascript. Now problem is when when i try to collect the data in the Textboxes which i added dynamically from server side, i am not able to do . Alos i tried to bedug, the total no of rows in Html table does not reflect the dynamically added rows.
2
1633
by: grawsha2000 | last post by:
Hello, How can I add a table to asp.net page dynamically with Code Behind style. I still can't find a way of doing it. This really causes me a big problem when I deal with records from database as I need to construct the table dynamically.
0
2395
by: Luis Esteban Valencia | last post by:
Hello I wrote a program with code behind in C# to add row into table dynamically and the program worked very well in .Net Framework 1.1. When I run this program in .Net Framework 2.0 beta version, the program is not working as in version 1.1. So what is the problem? Microsoft declared that the version 2.0 is fully backward support, but it is not. Is that the problem with 2.0 version? I find out the problem in version 2.0. After...
3
3145
by: Fao, Sean | last post by:
I have a DataGrid that I'm adding CheckBox controls to at runtime (in the code behind) and I'm not sure if I'm doing it correctly. First of all, I noticed that the MyDataGrid.Columns.Add() method expects a DataGridColumn so I instantiated an object of type TemplateColumn that I had hoped I could add a CheckBox to. I soon discovered that the ItemTemplate property of the TemplateColumn class returned an object that had implemented the...
2
3558
by: Flack | last post by:
Hey guys, I have a DataGrid and DataTable field in my class : private ImageDataGrid dataGrid1; //ImageDataGrid extends dataGrid and just overides OnMouseDown private DataTable dt = new DataTable("MyTable"); In the classes constructor, after InitializeComponent() I call the following
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.