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

DOM-dynamically-created HTML buttons and function argument passing problem

Buddies,
I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function

Expand|Select|Wrap|Line Numbers
  1. //////////////////////////////////////////////////////////////////////////////////// version 1 : using global variables ////////////////////////////////////////////////////////////////// 
  2. var _textField, _divColorPicker;  // global vars
  3.  
  4. window.onload = function()
  5. {
  6. _textField = document.getElementById('htxaMessage'); // fill global var  *****    
  7. _divColorPicker= document.getElementById('divColorPicker');  // fill global var  ******      
  8.  
  9.     formDivColorPicker();  // no need arguments ******************************
  10. }
  11.  
  12. function changeFontColor(button)
  13. {
  14.     var fontColor = button.id.split('_');
  15.     // insertAtCaret function is defined in some where else on the page
  16.     insertAtCaret(_textField, '[font color=' + fontColor[1] + ']', '[/font]');
  17.     alert(_divColorPicker.id);
  18. }
  19. function formDivColorPicker()
  20. {
  21.     var op = _divColorPicker;
  22.     var c=0;
  23.  
  24.     for(var i=0, l=JSONColors.length; i<l; i++)
  25.     {
  26.      // DOM-dynamically-created HTML buttons
  27.  
  28.         op.innerHTML += '<input id=' + 'btnColor_' + JSONColors[i].name + ' type=button onclick="changeFontColor(this);" style="width:16px; height:16px; background-color:' + JSONColors[i].value + '" />&nbsp&nbsp';
  29.  
  30.         c++;
  31.         if(c==COLOR_BUTTONS_PER_ROW)
  32.         {
  33.             op.innerHTML +="<br><span style='margin-left:150px'>";
  34.             c=0;
  35.             op.innerHTML +="</span>";
  36.         }
  37.     }
  38. }
  39.  
  40. … markups on the page 
  41.   <tr>
  42.                 <td>
  43.                      <div id="divColorPicker" style="display:none; margin-left:150px; position: static">
  44.                      </div>
  45.                 </td>
  46.             </tr>
  47.  
  48.             <tr style="height:40%">
  49.                 <td align="left" valign="top">
  50.                       <textarea id=htxaMessage cols="70" rows=3 onkeyup="htxaMessage_checkIfEnterKeyHit(event);"></textarea>
  51.  
  52. The codes work fine with the “changeFontColor” function if I use global variables such as var _textField, _divColorPicker;  on window load event and I need to pass this to that function when I create buttons using DOM in the “formDivColorPicker” function. 
  53. However, if I do not use global variables and the form of functions as follows, then I get javascript runtime error like “object is undefined…”  in function “changeFontColor” for arguments like textField and divColorPicker:
  54.  
  55.  
  56. /////////////////////////// version 2 not using global vars but argument passing  ////////
  57. //var _textField, _divColorPicker;  // commented out
  58.  
  59. window.onload = function()
  60. {
  61. var _textField = document.getElementById('htxaMessage'); // local var
  62. var _divColorPicker= document.getElementById('divColorPicker');// local var
  63.  
  64.     formDivColorPicker(_textField, _divColorPicker); // call the function with arguments ******************************
  65. }
  66.  
  67. function changeFontColor(textField, divColorPicker){
  68.     var fontColor = button.id.split('_');
  69.     // insertAtCaret function is defined in some where else on the page
  70.     insertAtCaret(_textField, '[font color=' + fontColor[1] + ']', '[/font]'); // error******
  71.     alert(divColorPicker.id); // error ******
  72. }
  73.  
  74. function formDivColorPicker(textField, divColorPicker){
  75.     var op = _divColorPicker;
  76.     var c=0;
  77.  
  78.     for(var i=0, l=JSONColors.length; i<l; i++)
  79.     {
  80.      // DOM-dynamically-created HTML buttons
  81.  
  82.      op.innerHTML += '<input id=' + 'btnColor_' + JSONColors[i].name + ' type=button onclick="changeFontColor(‘ + textField + ‘,’ + divColorPicker + ‘);" style="width:16px; height:16px; background-color:' + JSONColors[i].value + '" />&nbsp&nbsp';
  83.  
  84.         c++;
  85.         if(c==COLOR_BUTTONS_PER_ROW)
  86.         {
  87.             op.innerHTML +="<br><span style='margin-left:150px'>";
  88.             c=0;
  89.             op.innerHTML +="</span>";
  90.         }
  91.     }
  92. }

Thanks in advance for any help or idea.
johnjsforum
Aug 2 '07 #1
1 2240
acoder
16,027 Expert Mod 8TB
The underscores need to be removed on lines 71 and 76. You've forgotten to remove them to use the passed arguments.
Jan 1 '08 #2

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

Similar topics

2
by: John VanRyn | last post by:
Ok, I have XML like this (XML Exhibit A) that I read in from a file ala .. $doc->load("./Template.xml"); because I could not figure out how to set the !DOCTYPE element. ------ XML Exhibit A-----...
4
by: Carlo Sette | last post by:
Hi all, when I try to Parse a sample XML ducument Python display the follow error message: --------------------------------------------------------------------------- Traceback (most recent call...
4
by: Skip Montanaro | last post by:
I'm getting somewhat painfully acquainted with xml.dom.minidom. What is the relationship between its documentElement attribute and its childNodes list? I thought XML documents consisted of a...
0
by: Steve Roberts | last post by:
I want to write a graphical view of an XML tree, using a DOM interface, that allows the XML tree to be edited, with nodes being added or removed. For this I was going to create a basic tree...
5
by: Chris | last post by:
Hi, the docs say : "The Xml-document is not loaded into memory when using XmlTextReader, as opposed to using the DOM where the entire document is loaded in memory" but, when using...
5
by: segfalt | last post by:
Hey All, I'm wondering if someone can give me a hand with the following. I'm frequently updating xml files, sometimes in bulk updates. I'm having trouble getting the output formatted nicely...
4
by: YAD | last post by:
How do you decide which DOM level you should use? I've been told level 2 is the "modern" approach, but also saw a website that said use level 0 any time you can. Should you use higher-level code...
0
by: Greg Copeland | last post by:
I am attempting to freeze an application which uses the dom.minidom parser. When I execute my application, I get an import error of: ImportError: No module named dom.minidom. During the freeze...
9
by: Lie | last post by:
Why this generates AttributeError, then not? Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30) on linux2 Type "help", "copyright", "credits" or "license" for more information. Traceback (most...
10
by: Simon Willison | last post by:
I'm having a horrible time trying to get xml.dom.pulldom to consume a UTF8 encoded XML file. Here's what I've tried so far: <msg>Simon\xe2\x80\x99s XML nightmare</msg> """ ('START_DOCUMENT',...
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: 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...
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...

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.