![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, What I wanted to do is to call a function from a newly created element. * But it stumbled me. Here's the line that references the newly created element and I used the alert function for debugging for now. * Did I mess up all these quotes? // once again this is the key line of code, problem area, disregard var i etc... newTRc1.innerHTML ='\<input type="text" name=q'+i+' size="60"\>;Type: \<select name=qType'+i+ 'onchange="alert(i);"\>\<option value="YN" selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'; Thanks. |
#3
| |||
| |||
|
|
What I wanted to do is to call a function from a newly created element. But it stumbled me. Here's the line that references the newly created element and I used the alert function for debugging for now. Did I mess up all these quotes? |
|
// once again this is the key line of code, problem area, disregard var i etc... newTRc1.innerHTML ='\<input type="text" name=q'+i+' size="60"\>;Type: ^start end^ ^start \<select name=qType'+i+ 'onchange="alert(i);"\>\<option value="YN" end^ ^start[1] ^^^[2] selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'; ^end |
#4
| |||
| |||
|
|
On May 17, 6:52 pm, DL <tatata9... (AT) gmail (DOT) com> wrote: newTRc1.innerHTML ='\<input type="text" name=q'+i+' size="60"\>;Type: \<select name=qType'+i+ 'onchange="alert(i);"\>\<option value="YN" selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'; Ok, so, it looks like I wasn't thinking, the Event handler within the newly created element hasn't been loaded by a browser, hence, no action upon such an event. Yes? |
|
Or another better approach to achieve the same goal? |
#5
| |||
| |||
|
|
DL wrote: On May 17, 6:52 pm, DL <tatata9... (AT) gmail (DOT) com> wrote: newTRc1.innerHTML ='\<input type="text" name=q'+i+' size="60"\>;Type: \<select name=qType'+i+ 'onchange="alert(i);"\>\<option value="YN" selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'; Ok, so, it looks like I wasn't thinking, the Event handler within the newly created element hasn't been loaded by a browser, hence, no action upon such an event. Yes? Not quite. The event handler exists before, as native code. The event handler attribute value is a string of characters that is passed on to the script engine which uses it to generate a function (the primary event ^^^^^^^^^^^ |
|
listener) that is called by the event handler on event. |
#6
| |||
| |||
|
|
DL wrote: What I wanted to do is to call a function from a newly created element. * But it stumbled me. Here's the line that references the newly created element and I used the alert function for debugging for now. * Did I mess up all these quotes? Yes, you did. // once again this is the key line of code, problem area, disregard var i etc... newTRc1.innerHTML ='\<input type="text" name=q'+i+' size="60"\>;Type: * * * * * * * * * * *^start * * * * * * * * *end^ * ^start> \<select name=qType'+i+ 'onchange="alert(i);"\>\<option value="YN" * * * * * * * * * end^ * *^start[1] * * * ^^^[2]> selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *^end There. *Because you have not properly delimited all your attribute values [^1], you will generate * <select name=qType42onchange="alert(i);" if i == 42. *What do you expect of the user agent to do here? While delimiting all attribute values with " -- * <select name="qType42"onchange="alert(i);" -- or including the missing whitespace -- * <select name=qType42 onchange="alert(i);" -- or do both of them would be a quick fix, I strongly suggest you stop cooking proprietary (innerHTML) tag soup and start using DOM Level 2 creator and mutator methods instead. *With a few user-defined wrappers they can be even more efficient to use than what you have now. http://www.w3.org/TR/DOM-Level-2-Core/ Furthermore, if you generate the identifier of `i' instead of the current value of `i' [^2], it will be resolved on execution of the event handler attribute value, which is probably not what you want here. BTW, `<' and `>' do not need to be escaped in string literals; `</' (ETAGO) should be escaped as `<\/' if the source code is within an HTML document. |
#7
| |||
| |||
|
|
On May 17, 9:08*pm, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de wrote: DL wrote: What I wanted to do is to call a function from a newly created element. * But it stumbled me. Here's the line that references the newly created element and I used the alert function for debugging for now. * Did I mess up all these quotes? Yes, you did. // once again this is the key line of code, problem area, disregard var i etc... newTRc1.innerHTML ='\<input type="text" name=q'+i+' size="60"\>;Type: * * * * * * * * * * *^start * * * * * * * * *end^ * ^start> \<select name=qType'+i+ 'onchange="alert(i);"\>\<option value="YN" * * * * * * * * * end^ * *^start[1] * * * ^^^[2]> selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *^end There. *Because you have not properly delimited all your attribute values [^1], you will generate * <select name=qType42onchange="alert(i);" if i == 42. *What do you expect of the user agent to do here? While delimiting all attribute values with " -- * <select name="qType42"onchange="alert(i);" -- or including the missing whitespace -- * <select name=qType42 onchange="alert(i);" -- or do both of them would be a quick fix, I strongly suggest you stop cooking proprietary (innerHTML) tag soup and start using DOM Level 2 creator and mutator methods instead. *With a few user-defined wrappers they can be even more efficient to use than what you have now. http://www.w3.org/TR/DOM-Level-2-Core/ Furthermore, if you generate the identifier of `i' instead of the current value of `i' [^2], it will be resolved on execution of the event handler attribute value, which is probably not what you want here. BTW, `<' and `>' do not need to be escaped in string literals; `</' (ETAGO) should be escaped as `<\/' if the source code is within an HTML document.. Ok, many thanks. *Now, I changed part of it to ... <select name=qType'+i+' onchange="doCheckbox('+i+')"><option ... This event handler is now recognized by my browser (IE7), however, I'm getting "Object expected" error, fyi, the doCheckbox function (as a sub function) looks this: function doCheckbox(i) { * // debug * *alert(i); * // add a new checkbox here * /* ... */ }- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text - |
#8
| |||
| |||
|
|
On May 17, 9:47*pm, DL <tatata9... (AT) gmail (DOT) com> wrote: On May 17, 9:08*pm, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de wrote: DL wrote: What I wanted to do is to call a function from a newly created element. * But it stumbled me. Here's the line that references the newly created element and I used the alert function for debugging for now. * Did I mess up all these quotes? Yes, you did. // once again this is the key line of code, problem area, disregard var i etc... newTRc1.innerHTML ='\<input type="text" name=q'+i+' size="60"\>;Type: * * * * * * * * * * *^start * * * * * * * * *end^ * ^start> \<select name=qType'+i+ 'onchange="alert(i);"\>\<option value="YN" * * * * * * * * * end^ * *^start[1] * * * ^^^[2]> selected\>Yes/No\<option value="CK"\>Checkbox\</select\><br\>'; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *^end There. *Because you have not properly delimited all your attribute values [^1], you will generate * <select name=qType42onchange="alert(i);" if i == 42. *What do you expect of the user agent to do here? While delimiting all attribute values with " -- * <select name="qType42"onchange="alert(i);" -- or including the missing whitespace -- * <select name=qType42 onchange="alert(i);" -- or do both of them would be a quick fix, I strongly suggest you stop cooking proprietary (innerHTML) tag soup and start using DOM Level 2 creator and mutator methods instead. *With a few user-defined wrappers they can be even more efficient to use than what you have now. http://www.w3.org/TR/DOM-Level-2-Core/ Furthermore, if you generate the identifier of `i' instead of the current value of `i' [^2], it will be resolved on execution of the event handler attribute value, which is probably not what you want here. BTW, `<' and `>' do not need to be escaped in string literals; `</' (ETAGO) should be escaped as `<\/' if the source code is within an HTML document. Ok, many thanks. *Now, I changed part of it to ... <select name=qType'+i+' onchange="doCheckbox('+i+')"><option ... This event handler is now recognized by my browser (IE7), however, I'm getting "Object expected" error, fyi, the doCheckbox function (as a sub function) looks this: function doCheckbox(i) { * // debug * *alert(i); * // add a new checkbox here * /* ... */ }- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text - ok, I've fixed this problem by making the doCheckbox() a separate function . *Now, with the doCheckbox() function, If my code looks like the following, if (document.getElementById('qType'+i).value = 'CK') { * *var TBL = document.getElementById('tbl'); * *...} I got "document.getElementById(...)" is null or an object err but if I comment out the IF line if (document.getElementById('qType'+i).value = 'CK') then the code works but I lose the logic of adding the checkbox only when type is checkbox. *Why? Thanks.- Hide quoted text - |
#9
| |||
| |||
|
|
On May 17, 10:35 pm, DL <tatata9... (AT) gmail (DOT) com> wrote: If my code looks like the following, if (document.getElementById('qType'+i).value = 'CK') { var TBL = document.getElementById('tbl'); ...} I got "document.getElementById(...)" is null or an object err but if I ^^^^^^^^^^^^ |
|
comment out the IF line if (document.getElementById('qType'+i).value = 'CK') then the code works but I lose the logic of adding the checkbox only when type is checkbox. Why? Ahe, now I think I know why the above IF statement failed because it's not a simple value, the SELECT element may multiple values... |
#10
| |||
| |||
|
|
comment out the IF line if (document.getElementById('qType'+i).value = 'CK') then the code works but I lose the logic of adding the checkbox only when type is checkbox. *Why? Ahe, now I think I know why the above IF statement failed because it's not a simple value, the SELECT element may multiple values... No, it is because document.getElementById('qType'+i) cannot be evaluated to an object reference, as the error message says. *So either there exists no element with that ID (yet), or your markup is not Valid. You have also assigned a value (`=') when you wanted a comparison (`==' or `==='). BTW, full-quoting hundreds of unnecessary lines is not going to encourage people to read your postings, much less to reply to them. http://jibbering.com/faq/ |
![]() |
| Thread Tools | |
| Display Modes | |
| |