Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Simple DOM function fails in IE
Old 10-02-2007, 03:27 AM Simple DOM function fails in IE
Novice Talker

Posts: 10
Name: Anders
Location: Sweden
Trades: 0
I've just started learning W3C DOM and I wrote my first script yesterday that to my happiness worked in Firefox 2.0.0.7 but not in IE 6.0.
The idea for the script is to use a select dropdown list to decide the width of a square.

This is the code in the head:
code:
<script type="text/javascript" language="javascript">

function changeWidth(){
var selObj = document.getElementById("selectWidth");
var selWidth = selObj.options[selObj.selectedIndex].value;
var square = document.getElementById("testSquare");
square.style.width = selWidth;
}

</script>

And this is the code for the body:
code:
<form name=widthForm onChange="changeWidth();">
<select id="selectWidth" name="selectWidth">
<option value='200px'>200</option>
<option value='195px'>195</option>
<option value='190px'>190</option>
<option value='185px'>185</option>
</select>
</form>

<br />
<div id="testSquare" style="width:200px; height:200px;
background:#CCCC66; overflow: hidden;">
In this box there is a lot, and I do mean a lot, of text that
shall change as the width of the box changes.
</div>

Can I get this to work in version 5+ browsers by changing my code?
The problem is probably in the changeWidth() method

Thanks in advance!

__________________
You couldn't fool anyone on the foolingest day of your life even if you had an electrical fooling machine!
Svartgam is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-02-2007, 03:49 AM Re: Simple DOM function fails in IE
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Your problem don't lies in the changeWidth() function.
The problem is that IE don't recognize onchange events on a form, only on the elements of the form.
There is a chart of which browser support which event and where:
http://www.quirksmode.org/js/events_compinfo.html

Change your html by moving the onchange to the select, that way:
HTML Code:
<form name=widthForm >
<select id="selectWidth" name="selectWidth" onChange="changeWidth();">
<option value='200px'>200</option>
<option value='195px'>195</option>
<option value='190px'>190</option>
<option value='185px'>185</option>
</select>
</form>
<script type="text/javascript" language="javascript">

function changeWidth(){
var selObj = document.getElementById("selectWidth");
var selWidth = selObj.options[selObj.selectedIndex].value;
var square = document.getElementById("testSquare");
square.style.width = selWidth;
}

</script>
<br />
<div id="testSquare" style="width:200px; height:200px;
background:#CCCC66; overflow: hidden;">
In this box there is a lot, and I do mean a lot, of text that
shall change as the width of the box changes.
</div>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 10-02-2007, 05:30 AM Re: Simple DOM function fails in IE
Novice Talker

Posts: 10
Name: Anders
Location: Sweden
Trades: 0
Works like a charm, much obliged!
__________________
You couldn't fool anyone on the foolingest day of your life even if you had an electrical fooling machine!
Svartgam is offline
Reply With Quote
View Public Profile
 
Old 10-02-2007, 05:23 PM Yet another question
Novice Talker

Posts: 10
Name: Anders
Location: Sweden
Trades: 0
I have another DOM-question;

Is it possible to have the user type text into an input field and simultaneously display that text in a div box somewhere else on the page and continuosly update it as the user continues to type and/or erase text?
If so, could anyone pleae point me in the right direction?


P.S.
I don't know what the standard procedure is for posting here since I'm relatively new but instead of creating a new thread about a slightly similar subject I will continue on this one. Should that decision be frowned upon by moderators, please rename this thread D.S.
__________________
You couldn't fool anyone on the foolingest day of your life even if you had an electrical fooling machine!
Svartgam is offline
Reply With Quote
View Public Profile
 
Old 10-02-2007, 06:20 PM Re: Simple DOM function fails in IE
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
I don't know what the standard procedure is
Simply put, create a thread per questions.

Aside this, for what you are looking, you will need to rely on the onkeypress event.
Quite easy:
HTML Code:
<html>
  <body>
    <textarea rows="5" cols="60" onkeypress="javascript:updField(this.value);">Please type your text here</textarea>
    <br/>
    <div id="textTarget" style="width:300px;height:200px;border:1px solid red"></div>
    <script type="text/javascript">
      function updField(txt){
        var trg=document.getElementById('textTarget');
        //those 2 lines to rebuild the carriage return in HTML
        txt=txt.replace(/\r/gi, "");
        txt=txt.replace(/\n/gi, "<br/>");
        trg.innerHTML=txt;
      }
    </script>
  </body>
</html>
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 10-02-2007 at 06:33 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 10-03-2007, 04:23 AM Re: Simple DOM function fails in IE
Novice Talker

Posts: 10
Name: Anders
Location: Sweden
Trades: 0
Ok, then next time I will crete a new thread.
Thanks for the code tripy, it worked good except there was a delay on the change taking place so I changed to onkeyup and it works like a charm!
__________________
You couldn't fool anyone on the foolingest day of your life even if you had an electrical fooling machine!
Svartgam is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Simple DOM function fails in IE
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17889 seconds with 12 queries