Monday, 18 March 2013

Tips and tricks


Problem
The select object's value property doesn't give the selected value

Browser
Internet Explorer

Example
The select drop down:
[HTML]<select name="test" id="test">
<option>1
<option>2
</select>[/HTML]
The Javascript code:
Expand|Select|Wrap|Line Numbers
  1. var selObj = document.getElementById("test");
  2. var val = selObj.value;
Solution
Give values to the option elements:
[HTML]<select name="test" id="test">
<option value="1">1
<option value="2">2
</select>[/HTML]
Alternative Solution
Change the Javascript to:
Expand|Select|Wrap|Line Numbers
  1. var selObj = document.getElementById("test");
  2. var val = selObj.options[selObj.selectedIndex].text;
Aug 18 '07 #1
Share this Article
  Share on Google+ 
9 Comments

Save upto 80% on Domain Names with Free Email Ids & Privacy Protect

acoder
ExpertMod15k+
P: 16,002
The table row not getting added to the table in IE is a common problem. Well, here's the answer: add it to the tbody instead. Who would've thought IE would be so rigid?
Sep 2 '07 #2

acoder
ExpertMod15k+
P: 16,002
Two more bugs for IE's buggy (i.e. non-standard) implementation of the select element's add() method.
Sep 3 '07 #3

acoder
ExpertMod15k+
P: 16,002
IE doesn't respect "checked" property when dynamically adding checkboxes. Added to the list.
Sep 5 '07 #4

acoder
ExpertMod15k+
P: 16,002
Opera has a quirk relating to onload and onunload which may catch some people out. It's useful to know in case you have some functionality which is triggered by one of these two events. Oh yes, and two more IE bugs with many more to follow I'm sure.
Nov 1 '07 #5

cheogt
P: 5
Problem
Not possible to prototype the Object() object
Browser
Internet Explorer
Example
The Javascript code:
Expand|Select|Wrap|Line Numbers
  1. //create an Alias (via protoype) for getElementById, or getElementByTagName
  2. Object.prototype.byTag=Object.prototype.getElementsByTagName;
  3. t=document.byTag('input');
Some relevant HTML:
Expand|Select|Wrap|Line Numbers
  1. <input id="anyName1" type="text" ...>
  2. <input id="anyName2" type="text" ...>
Solution
Create a simple function, and use the object as parameter (not nice!)... :
Expand|Select|Wrap|Line Numbers
  1. function byTag(o,s) {
  2.   return o.getElementsByTagName(s)
  3. }
  4. t=byTag(document,'input');
Nov 15 '07 #6

acoder
ExpertMod15k+
P: 16,002
Problem
Not possible to prototype the Object() object
Yes, but is it such a good idea? See Object.prototype is verboten (I don't know where the fascination with German words started!)
Dec 6 '07 #7

hsriat
Expert100+
P: 1,653
Problem
Not able to use innerHTML for a dynamically created row.

Browser
Internet Explorer

Example
Expand|Select|Wrap|Line Numbers
  1. var rowX = tableObject.insertRow(tableObject.rows.length);
  2. rowX.innerHTML = '<td id="td01" class="cells" onclick="doSomething()">This is the cell</td>';
Solution
Use insertCell()
Expand|Select|Wrap|Line Numbers
  1. var rowX = tableObject.insertRow(tableObject.rows.length);
  2. var cellX = rowX.insertCell(0);
  3. cellX.id = "td01";
  4. cellX.className = 'cells';
  5. cellX.onclick = function () {
  6.     doSomething()
  7. }
  8. cellX.innerHTML = 'This is the cell';
May 14 '08 #8

acoder
ExpertMod15k+
P: 16,002
Problem
Not able to use innerHTML for a dynamically created row.
Spot on. However, if you're going to use insertRow(), most likely you'd use insertCell too. Good to know all the same. Thanks.
May 17 '08 #9

acoder
ExpertMod15k+
P: 16,002
Split out each one into their own page. Easier to read and link to.
Jun 8 '08 #10

No comments:

Post a Comment

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...