javascript

That's because "09" is an invalid number, in octal.

The parseInt() funciton acutally has 2 arguments that most people are not aware of, first is the string to parse and then a radix (which is optional). The radix value allows you to convert a binary (base 2), hexadecimal (base 16) or other base string to a decimal integer.

Example: parseInt("FF", 16); returns 255

The problem is that if you leave the radix argument off the function doesn't necessarily assume you want a decimal (base 10) conversion. Instead it checks the input string (the first argument) and if it starts with "0x" it assumes it's a hexadecimal value. If it starts with "0" - not followed by an "x" - it takes it as an octal value. This follows the JavaScript convention for numeric constants.

If you code

var x = 0x18;
alert(x);

it will display 24 which is the decimal equivalent of the hex number "18". Likewise,

var x = 014;
alert(x);

displays 12 which is the decimal value of the octal number "14".

Resource: FAQTs

Hexadecimal uses the numbers 0 to 9 and the letters A to F, 16 in all. You would normally see hexadecimal values used for colors (Orange: #FF9C00, White: #FFFFFF, or Red: #FF0000). Octal is base 8, so only the numbers 0-7 are valid. Hence, "09" is not a valid octal number and the function returns 0. Just as it would for "abc" in decimal ... it's not a valid number.

To avoid this, get into the habit of always adding the second argument, in this case ...

parseInt("09", 10);

returns 9 (decimal), as desired.

Resources
* FAQTs

That's because "09" is an invalid number, in octal.

The parseInt() funciton acutally has 2 arguments that most people are not aware of, first is the string to parse and then a radix (which is optional). The radix value allows you to convert a binary (base 2), hexadecimal (base 16) or other base string to a decimal integer.

Example: parseInt("FF", 16); returns 255

The problem is that if you leave the radix argument off the function doesn't necessarily assume you want a decimal (base 10) conversion. Instead it checks the input string (the first argument) and if it starts with "0x" it assumes it's a hexadecimal value. If it starts with "0" - not followed by an "x" - it takes it as an octal value. This follows the JavaScript convention for numeric constants.

If you code

var x = 0x18;
alert(x);

it will display 24 which is the decimal equivalent of the hex number "18". Likewise,

var x = 014;
alert(x);

displays 12 which is the decimal value of the octal number "14".

Resource: FAQTs

Hexadecimal uses the numbers 0 to 9 and the letters A to F, 16 in all. You would normally see hexadecimal values used for colors (Orange: #FF9C00, White: #FFFFFF, or Red: #FF0000). Octal is base 8, so only the numbers 0-7 are valid. Hence, "09" is not a valid octal number and the function returns 0. Just as it would for "abc" in decimal ... it's not a valid number.

To avoid this, get into the habit of always adding the second argument, in this case ...

parseInt("09", 10);

returns 9 (decimal), as desired.

Resources
* FAQTs

Normally you would select an option with a server-side script like PHP which is a lot faster and doesn't require the client's browser to do the work. But on the rare occasion where you need to set an option with JavaScript, here's a small script on how to do it.

 
document.getElementById('select_box_id_here').value
 = 'the value of the one you want selected';
 

This will work if you have constructed the page the proper way. On the odd chance you've used JavaScript to pull in a whole string of HTML then use the method below.

 
var selectBox = document.getElementById('select_box_id_here');
for(i=0; i
<selectBox.options.length; i++) {
	if(selectBox.options[i].value == a_value) {
		selectBox.options[i].selected = true;
		i = selectBox.options.length;
	}
}
 

The above script simply loops through your select box and determines if the current option (in the loop) matches the value (a_value), if so then set the option to selected. The line "i = selectBox.options.length;" simply stops the 'for loop' from checking any further options.

The reason why you have to manually go through the select box yourself is because the DOM doesn't actually know the select box is on the page when you've just dumped a whole lot of HTML into the page with JavaScript. If it's possible to refresh the DOM then let me know, otherwise the above option is the solution to this problem.

Just a small tip for some people who might be modifying arrays during a loop, where the array is used in the condition of the loop. Just be a little bit careful on what you do with the array when looping through.

 
var testArray = Array('1','2','3','4','5');
for(var j=0; j<testArray.length; j++) {
    alert(testArray[j]);
    if(testArray[j] == '3') {
        testArray.splice(j,1);
    }
}
 

The above array loop is written in JavaScript, whether you're using JavaScript or not this is still something small to keep in mind. It will produce an alert of '1', then '2', then '3', and then '5'.

The length of the above array is 5, you start at position 0 and testArray[0] doesn't equal '3' so move on, same thing with position 1 (testArray[1] doesn't equal '3'). You then move onto position 2 where testArray[2] does equal '3', so we splice the array at position 2 which removes testArray[2] from the array, making the rest of the array drop down a position.

j still equals 2, so when it loops through again, j will equal 3. When it looks at what position 3 equals (testArray[3]) it will now be '5' as the array's position dropped down by one from the splice.

So we missed out on alerting '4' after going through that loop and you might be doing something important with the loop rather then simply alerting the values, there is 2 ways to get around this. One, put the 'something important' function after the loop in its own separate array. Or two, even easier, if the 'if condition' is true and you modified the array, simply affect the value of j so that the next loop through will be correct (like below).

 
var testArray = Array('1','2','3','4','5');
for(var j=0; j<testArray.length; j++) {
    alert(testArray[j]);
    if(testArray[j] == '3') {
        testArray.splice(j,1);
        j--;
    }
}
 

What is TinyMCE?
TinyMCE is a platform independent web based Javascript HTML WYSIWYG editor control released as Open Source under LGPL by Moxiecode Systems AB. It has the ability to convert HTML TEXTAREA fields or other HTML elements to editor instances. TinyMCE is very easy to integrate into other Content Management Systems. Please visit their website for more details.

 
tinyMCE.init({ //settings go here });
 

Starting TinyMCE

 
tinyMCE.triggerSave(false,true);
tiny_mce_editor = tinyMCE.get('your_textarea_name');
var newData = tiny_mce_editor.getContent();
tinyMCE.execCommand( 'mceRemoveControl', false, 'your_textarea_name' );
 

Getting the Textarea Text and Closing TinyMCE
The new data that the user would enter into TinyMCE is now stored in the variable newData.

You need to specify a trigger save like this for 2 reasons:-
* You can't simply get content from your textarea once TinyMCE has taken over it, as the text in the textarea isn't actually stored in that field name you know.
* You need to remove the control properly so that TinyMCE doesn't make another instance of the same textarea, or otherwise you will run into issues.