/    Sign up×
Community /Pin to ProfileBookmark

Any helpful ideas?

I am developing a script using HTML, CSS and Javascript that will allow:

  • 1. A question to be displayed

  • 2. A place for a student to respond. I want to use special Greek characters to form some mathematical equations and have added an array of common characters for the student to click on and insert into the textarea of their response.
    And finally,

  • 3. A display of the correct answer after the student response.
  • The draft script below accomplished all 3 of the above items in the Netscape and FireFox browsers. I can enter the special characters into the textarea and I get no errors in the ‘javascript:’ console area.

    The script loads and reports no errors in the Internet Explorer browser, but it does not work when buttons are pressed to insert special characters for the Internet Explorer browser. Again, I have no error reported??? It just does nothing but stare back at me. I don’t know where to start to fix the problem, whatever the problem is!!!

    Does anyone have any suggestions as to what to correct to make it work in all broswers? Any ideas on what to change since I am not getting any errors?

    Thanks in advance.

    [CODE]
    <HTML>
    <HEAD>
    <META http-equiv=Content-Type content=”text/html; charset=iso-8859-1″>

    <style type=”text/css”>
    <!–
    .para {font-family: “Verdana”,”Arial”,”sans-serif”; font-size:12pt;}
    a.lnk {background-repeat:no-repeat;font-family:”Verdana”,”arial”,”sans-serif”;font-size:12px;}
    input, textarea
    {
    color: green;
    font-family: Verdana,Arial,Sans-serif;
    font-size: 0.8em;
    }
    –>
    </style>

    <script type=”text/javascript”>
    <!– Begin
    var page = “”;
    var btns = new Array(916,931,936,945,946,947,948,949,952,955,956,957,960,961,963,964,966,969);
    var btnN = new Array(‘Delta’,’Sum’,’psi’,’alpha’,’beta’,’gamma’,’delta’,’epsilon’,’theta’,
    ‘lamda’,’mu’,’nu’,’eta’,’rho’,’sigma’,’tau’,’phi’,’omega’)
    // Put the id’s of your hidden divs in this array
    var Qsubs_array = new Array(“qsub1″,”qsub2”); // questions
    var Rsubs_array = new Array(“rsub1″,”rsub2”); // responses
    var Asubs_array = new Array(“asub1″,”asub2″); // answers

    /* the following section is not used at this time
    function displayQSubs(the_sub){
    if (document.getElementById(the_sub).style.display==””){
    document.getElementById(the_sub).style.display = “none”;return }
    for (i=0;i<Qsubs_array.length;i++){
    var my_sub = document.getElementById(Qsubs_array[i]);
    my_sub.style.display = “none”; }
    document.getElementById(the_sub).style.display = “”;
    }
    */

    function displayRSubs(the_sub){
    if (document.getElementById(the_sub).style.display==””){
    document.getElementById(the_sub).style.display = “none”;return }
    for (i=0;i<Rsubs_array.length;i++){
    var my_sub = document.getElementById(Rsubs_array[i]);
    my_sub.style.display = “none”; }
    document.getElementById(the_sub).style.display = “”;
    }

    function displayASubs(the_sub){
    if (document.getElementById(the_sub).style.display==””){
    document.getElementById(the_sub).style.display = “none”;return }
    for (i=0;i<Asubs_array.length;i++){
    var my_sub = document.getElementById(Asubs_array[i]);
    my_sub.style.display = “none”; }
    document.getElementById(the_sub).style.display = “”;
    }

    function InsertText(input,theText) {
    if (typeof input.selectionStart != ‘undefined’) {
    var start = input.selectionStart;
    var end = input.selectionEnd;
    input.value = input.value.substr(0,start) + theText + input.value.substr(end);
    input.selectionStart = start + theText.length;
    input.selectionEnd = start + theText.length;
    input.focus();
    }
    }
    // –>
    </SCRIPT>

    </HEAD>
    <BODY>
    <FORM name=faqform onSubmit=”javascript:void(0);return false”>

    <div class=”para” id=”asub0″ style=”display:none”><textarea name=Ans0 cols=60 rows=8 readOnly>
    Question #1
    </textarea></div>
    Question #1

    <P>
    <TABLE border=1 width=”100%”>
    <TR>
    <TD width=”25%” valign=”top”>
    Solution: <P>
    <div class=”para” id=”qsub1″>
    <a href=”javascript:void(0)” class=”lnk” onClick=”displayRSubs(‘rsub1’)”>
    Step #1?</a> </div>

    <p>
    <div class=”para” id=”qsub2″>
    <a href=”javascript:void(0)” class=”lnk” onClick=”displayRSubs(‘rsub2’)”>
    Step #2?</a> </div>
    </TD>

    <TD valign=”top”>
    Student Response<P>
    <div class=”para” id=”rsub1″ style=”display:none”>
    <TEXTAREA name=student1 cols=60 rows=8 wrap=virtual>Student Response to Step #1</TEXTAREA>
    <script language=”javascript”>
    page = ”;
    for (var i=0; i<btns.length; i++) {
    if ((i % 6) == 0) { page += ‘<br>’; }
    page += “<button onClick=”InsertText(student1,’&#”;
    page += btns[i]+”;’)”>&#”+btns[i]+”; (“+btnN[i]+”)</button>n”;
    }
    page += “<br>”;
    page += “<button onClick=”InsertText(student1,'<sub>’)”>&lt;sub&gt;</button>”;
    page += “<button onClick=”InsertText(student1,'</sub>’)”>&lt;/sub&gt;</button>”;
    page += “<button onClick=”InsertText(student1,'<sup>’)”>&lt;sup&gt;</button>”;
    page += “<button onClick=”InsertText(student1,'</sup>’)”>&lt;/sup&gt;</button>”;
    page += “<button onClick=”InsertText(student1,’¹/’)”>¹/</button>”;
    document.writeln(page);
    </script>
    </div>
    <div class=”para” id=”rsub2″ style=”display:none”>
    <TEXTAREA name=student2 cols=60 rows=8 wrap=virtual>
    Step #2
    Student Response</TEXTAREA>
    <script language=”javascript”>
    page = ”;
    for (var i=0; i<btns.length; i++) {
    if ((i % 6) == 0) { page += ‘<br>’; }
    page += “<button onClick=”InsertText(student2,’&#”;
    page += btns[i]+”;’)”>&#”+btns[i]+”; (“+btnN[i]+”)</button>n”;
    }
    page += “<br>”;
    page += “<button onClick=”InsertText(student2,'<sub>’)”>&lt;sub&gt;</button>”;
    page += “<button onClick=”InsertText(student2,'</sub>’)”>&lt;/sub&gt;</button>”;
    page += “<button onClick=”InsertText(student2,'<sup>’)”>&lt;sup&gt;</button>”;
    page += “<button onClick=”InsertText(student2,'</sup>’)”>&lt;/sup&gt;</button>”;
    page += “<button onClick=”InsertText(student2,’¹/’)”>¹/</button>”;
    document.writeln(page);
    </script>
    </div>
    </TD></TR>

    <TR><TD>
    Answer:<br>

    <a href=”javascript:void(0)” class=”lnk” onClick=”displayASubs(‘asub1’)”> Step 1?</a>

    <p>
    <a href=”javascript:void(0)” class=”lnk” onClick=”displayASubs(‘asub2’)”> Step 2?</a>
    </TD><TD>
    <div class=”para” id=”asub1″ style=”display:none”>
    <textarea name=Ans1 cols=60 rows=8 readOnly>
    Correct Answer Step #1</textarea>
    </div>

    <div class=”para” id=”asub2″ style=”display:none”>
    <textarea name=Ans2 cols=60 rows=8 readOnly>
    Correct Answer
    Step #2
    </textarea>
    </div>
    </TD></TR>
    </TABLE>

    </FORM>
    </BODY>
    </HTML>

    [/CODE]

    to post a comment
    JavaScript

    3 Comments(s)

    Copy linkTweet thisAlerts:
    @konithomimoMar 15.2006 — here is your code updated . . . with a much easier InsertText function. Also, your code could probably be cut down quite a bit and simplified a lot.
    &lt;HTML&gt;
    &lt;HEAD&gt;
    &lt;META http-equiv=Content-Type content="text/html; charset=iso-8859-1"&gt;

    &lt;style type="text/css"&gt;
    &lt;!--
    .para {font-family: "Verdana","Arial","sans-serif"; font-size:12pt;}
    a.lnk {background-repeat:no-repeat;font-family:"Verdana","arial","sans-serif";font-size:12px;}
    input, textarea
    {
    color: green;
    font-family: Verdana,Arial,Sans-serif;
    font-size: 0.8em;
    }
    --&gt;
    &lt;/style&gt;

    &lt;script type="text/javascript"&gt;
    &lt;!-- Begin
    var page = "";
    var btns = new Array(916,931,936,945,946,947,948,949,952,955,956,957,960,961,963,964,966,969);
    var btnN = new Array('Delta','Sum','psi','alpha','beta','gamma','delta','epsilon','theta',
    'lamda','mu','nu','eta','rho','sigma','tau','phi','omega')
    // Put the id's of your hidden divs in this array
    var Qsubs_array = new Array("qsub1","qsub2"); // questions
    var Rsubs_array = new Array("rsub1","rsub2"); // responses
    var Asubs_array = new Array("asub1","asub2"); // answers

    /* the following section is not used at this time
    function displayQSubs(the_sub){
    if (document.getElementById(the_sub).style.display==""){
    document.getElementById(the_sub).style.display = "none";return }
    for (i=0;i&lt;Qsubs_array.length;i++){
    var my_sub = document.getElementById(Qsubs_array[i]);
    my_sub.style.display = "none"; }
    document.getElementById(the_sub).style.display = "";
    }
    */

    function displayRSubs(the_sub){
    if (document.getElementById(the_sub).style.display==""){
    document.getElementById(the_sub).style.display = "none";return }
    for (i=0;i&lt;Rsubs_array.length;i++){
    var my_sub = document.getElementById(Rsubs_array[i]);
    my_sub.style.display = "none"; }
    document.getElementById(the_sub).style.display = "";
    }

    function displayASubs(the_sub){
    if (document.getElementById(the_sub).style.display==""){
    document.getElementById(the_sub).style.display = "none";return }
    for (i=0;i&lt;Asubs_array.length;i++){
    var my_sub = document.getElementById(Asubs_array[i]);
    my_sub.style.display = "none"; }
    document.getElementById(the_sub).style.display = "";
    }

    function InsertText(theText, ins) {
    var mytext = document.getElementById('student1');
    if(ins==1)
    mytext = document.getElementById('student2');
    var myval = mytext.value;
    myval += theText;
    mytext.value = myval;
    }

    // --&gt;
    &lt;/SCRIPT&gt;

    &lt;/HEAD&gt;
    &lt;BODY&gt;
    &lt;FORM name=faqform onSubmit="javascript:void(0);return false"&gt;

    &lt;div class="para" id="asub0" style="display:none"&gt;&lt;textarea name=Ans0 cols=60 rows=8 readOnly&gt;
    Question #1
    &lt;/textarea&gt;&lt;/div&gt;
    Question #1

    &lt;P&gt;
    &lt;TABLE border=1 width="100%"&gt;
    &lt;TR&gt;
    &lt;TD width="25%" valign="top"&gt;
    Solution: &lt;P&gt;
    &lt;div class="para" id="qsub1"&gt;
    &lt;a href="javascript:void(0)" class="lnk" onClick="displayRSubs('rsub1')"&gt;
    Step #1?&lt;/a&gt; &lt;/div&gt;

    &lt;p&gt;
    &lt;div class="para" id="qsub2"&gt;
    &lt;a href="javascript:void(0)" class="lnk" onClick="displayRSubs('rsub2')"&gt;
    Step #2?&lt;/a&gt; &lt;/div&gt;
    &lt;/TD&gt;

    &lt;TD valign="top"&gt;
    Student Response&lt;P&gt;
    &lt;div class="para" id="rsub1" style="display:none"&gt;
    &lt;TEXTAREA name=student1 id="student1" cols=60 rows=8 wrap=virtual&gt;Student Response to Step #1&lt;/TEXTAREA&gt;
    &lt;script language="javascript"&gt;
    page = '';
    for (var i=0; i&lt;btns.length; i++) {
    if ((i % 6) == 0) { page += '&lt;br&gt;'; }
    page += "&lt;button onClick="InsertText('&amp;#";
    page += btns[i]+";',0)"&gt;&amp;#"+btns[i]+"; ("+btnN[i]+")&lt;/button&gt;n";
    }
    page += "&lt;br&gt;";
    page += "&lt;button onClick="InsertText(student1,'&lt;sub&gt;')"&gt;&amp;lt;sub&amp;gt;&lt;/button&gt;";
    page += "&lt;button onClick="InsertText(student1,'&lt;/sub&gt;')"&gt;&amp;lt;/sub&amp;gt;&lt;/button&gt;";
    page += "&lt;button onClick="InsertText(student1,'&lt;sup&gt;')"&gt;&amp;lt;sup&amp;gt;&lt;/button&gt;";
    page += "&lt;button onClick="InsertText(student1,'&lt;/sup&gt;')"&gt;&amp;lt;/sup&amp;gt;&lt;/button&gt;";
    page += "&lt;button onClick="InsertText(student1,'¹/')"&gt;¹/&lt;/button&gt;";
    document.writeln(page);
    &lt;/script&gt;
    &lt;/div&gt;
    &lt;div class="para" id="rsub2" style="display:none"&gt;
    &lt;TEXTAREA name=student2 id="student2" cols=60 rows=8 wrap=virtual&gt;
    Step #2
    Student Response&lt;/TEXTAREA&gt;
    &lt;script language="javascript"&gt;
    page = '';
    for (var i=0; i&lt;btns.length; i++) {
    if ((i % 6) == 0) { page += '&lt;br&gt;'; }
    page += "&lt;button onClick="InsertText('&amp;#";
    page += btns[i]+";',1)"&gt;&amp;#"+btns[i]+"; ("+btnN[i]+")&lt;/button&gt;n";
    }
    page += "&lt;br&gt;";
    page += "&lt;button onClick="InsertText(student2,'&lt;sub&gt;')"&gt;&amp;lt;sub&amp;gt;&lt;/button&gt;";
    page += "&lt;button onClick="InsertText(student2,'&lt;/sub&gt;')"&gt;&amp;lt;/sub&amp;gt;&lt;/button&gt;";
    page += "&lt;button onClick="InsertText(student2,'&lt;sup&gt;')"&gt;&amp;lt;sup&amp;gt;&lt;/button&gt;";
    page += "&lt;button onClick="InsertText(student2,'&lt;/sup&gt;')"&gt;&amp;lt;/sup&amp;gt;&lt;/button&gt;";
    page += "&lt;button onClick="InsertText(student2,'¹/')"&gt;¹/&lt;/button&gt;";
    document.writeln(page);
    &lt;/script&gt;
    &lt;/div&gt;
    &lt;/TD&gt;&lt;/TR&gt;

    &lt;TR&gt;&lt;TD&gt;
    Answer:&lt;br&gt;

    &lt;a href="javascript:void(0)" class="lnk" onClick="displayASubs('asub1')"&gt; Step 1?&lt;/a&gt;

    &lt;p&gt;
    &lt;a href="javascript:void(0)" class="lnk" onClick="displayASubs('asub2')"&gt; Step 2?&lt;/a&gt;
    &lt;/TD&gt;&lt;TD&gt;
    &lt;div class="para" id="asub1" style="display:none"&gt;
    &lt;textarea name=Ans1 cols=60 rows=8 readOnly&gt;
    Correct Answer Step #1&lt;/textarea&gt;
    &lt;/div&gt;

    &lt;div class="para" id="asub2" style="display:none"&gt;
    &lt;textarea name=Ans2 cols=60 rows=8 readOnly&gt;
    Correct Answer
    Step #2
    &lt;/textarea&gt;
    &lt;/div&gt;
    &lt;/TD&gt;&lt;/TR&gt;
    &lt;/TABLE&gt;

    &lt;/FORM&gt;
    &lt;/BODY&gt;
    &lt;/HTML&gt;
    Copy linkTweet thisAlerts:
    @JMRKERauthorMar 15.2006 — Thanks konithomino.

    . . . with a much easier InsertText function. [/QUOTE]
    I agree that your 'InsertText' function is much easier and it does work in IE, but I also lost a lot functionality. In fact, this was the code I first used when I started the project. I used the google 'search' function and found the code that allows insertion entry into any area on the <textarea> section.

    With your code, the new button entries ONLY go to the end of the textarea, not anywhere the cursor was positioned in the original version (working in NS and FF). I want the student to be able to insert the special characters anywhere within the textarea box, not just the end. I think this is important because the student will sometimes need to correct the current textbox entry, but will not want to repeat all the typing for a few inserted special characters.

    Do you have any ideas what part of your change makes the thing work in IE, where my code would not put it anywhere within the <textarea> when the buttons are clicked? I come back to the same problem, when I don't get errors I don't know what to fix or change. ?

    I also noticed that I would need to change the other "InsertText" calls for the subscript and superscript buttons. I assume this was an oversight in the 'updated' code. This comment leads nicely into the next area because I like common functions as it reduces my correction of typing errors significantly.


    Also, your code could probably be cut down quite a bit and simplified a lot.[/QUOTE]
    I also agree that my code is somewhat bloated at this time. Trying to come up with some "common code" function was going to be my next question. For example, I don't like the idea that I need to repeat the button assignment code for each section of the textbox entry.

    I would also like to make one 'display_Subs()' function where '_ = Q,R,A' since the only difference in the coding is the call to the appropriate textarea section. Any ideas here will also be appreciated.

    I'll try to see if I can understand your code about the 'getElementById()' section within the new 'InsertText()' function to use just one function call with different arguments rather that repeat the code several times. Maybe I can make this work in both the 'InsertText()' and 'display_Subs()' functions ?

    I thought I should get the code working in all three browsers before I started to trim it down. I want the script to be functional in the main browsers first, but I welcome any thoughts on these other topics as well.

    Thanks for trying. :o
    Copy linkTweet thisAlerts:
    @JMRKERauthorMar 16.2006 — With very slight modifications, I found the answer to my problem here: ?

    http://www.alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript/

    It performs the same actions in NS, FF and IE.
    ×

    Success!

    Help @JMRKER spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...