![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||||||
| |||||||
|
|
On Jun 13, 8:59 am, SAM <stephanemoriaux.NoAd... (AT) wanadoo (DOT) fr.invalid wrote: [...] script type='text/javascript' function $(id) // multi browser getter of an element There has recently been discussion about using $ as a function name, it is not considered a good idea around here: "Replacing document.getElementById with $ (dollar sign)" URL http://groups.google.com.au/group/co...8f81 84cd5a4e |
|
{ return typeof id != 'string'? id : document.getElementById? document.getElementById(id) : document.all? document.all[id] : document.forms[0].elements[id]; // suppose an alone form } A more efficient way is to test the supported features and create an appropriate function up front, then you don't have to test for getElementById and all every time, e.g. |
|
var getElement = (function() { if (document) { if (document.getElementById) { return function(id) { return (typeof id == 'string')? document.getElementById(id) : id; } } else if (document.all) { return function(id) { return (typeof id == 'string')? document.all(id) : id; } } } })(); |
|
function ConfirmDeleteImage() // would have to work with any browser { var cbo = $('cboUnitTypes'); if (cbo.options.length <= 0) I can't imagine a scenario where the length of an options collection |
|
can be less than zero. Anyway, the length of an options collection is an unsigned long so it can't be negative - providing the UA is standards comliant of course. :-) |
|
{ alert('I do not understand why the function did fire'); return false; } if (cbo.selectedIndex == 0) This test should be less than or equal to zero since the selectedIndex should be -1 if no option has been selected. |
|
{ alert('Choice another item in the list'); Choose? :-) |
#12
| |||
| |||
|
|
SAM wrote: [...] script type='text/javascript' function $(id) // multi browser getter of an element [...] { return typeof id != 'string'? id : document.getElementById? document.getElementById(id) : document.all? document.all[id] : document.forms[0].elements[id]; // suppose an alone form } A more efficient way is to test the supported features and create an appropriate function up front, then you don't have to test for getElementById and all every time, e.g. var getElement = (function() { if (document) { if (document.getElementById) { return function(id) { return (typeof id == 'string')? document.getElementById(id) : ^^^^^^^^^^^^^^^^^^^^^^^^ id; } } else if (document.all) { return function(id) { return (typeof id == 'string')? document.all(id) : id; ^^^^^^^^^^^^^^^^^^^^^^^^ } } } })(); |
![]() |
| Thread Tools | |
| Display Modes | |
| |