Two questions regarding event handler and keyCode

D

DL

Hi,

Here I have two problems/questions with regard to List function
(ordered and unordered) and I've pasted complete code below. My test
browser is IE7 on Windows XP.

Problem 1:
The event handler via the BUTTON element works (1st element within the
form) while using onclick event handler of inline script per <input
type="button" onclick.../> failed. Why? How to fix it?

Problem 2:
keyCode of 13 (key down), while it works fine I'd like to exit the
List function if user hit key down twice, what's the keyCode for
pressing key down twice? If no such keyCode, then how to achieve the
same outcome?

Thanks.

Complete code below
-----------------------------
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
function addList(obj,lt) {
var theBody = document.getElementById(obj);
if (lt == 'ol') {var listType = document.createElement('ol')};
if (lt == 'ul') {var listType = document.createElement('ul')};
theBody.appendChild(listType);

var li = document.createElement('li');
listType.appendChild(li);

//var keyCode = window.event.keyCode;

function chkKey() {
if (event.keyCode == 13) {
//{alert('carriage return')};

var newNode = document.createElement('li');
listType.appendChild(newNode);

}
}

document.onkeydown = chkKey;
}
</script>

</head>
<body>
<form method="post">
<button onclick="addList('txt','ol')">Order List</button>
<button onclick="addList('txt','ul')">Unordered List</button><br/>
<input type="button" onclick="var theBody =
document.getElementById('txt');var listType =
document.createElement('ol');theBody.appendChild(listType);var li =
document.createElement('li');listType.appendChild(li);function
chkKey() {if (event.keyCode == 13) {var newNode =
document.createElement('li');listType.appendChild(newNode);}}document.onkeydown
= chkKey;}" value="OL"/><br/>
<textarea name="txt" id="txt" cols="60" rows="12"></textarea>
</form>

</body>
</html>
 
D

DL

Hi,

Here I have two problems/questions with regard to List function
(ordered and unordered) and I've pasted complete code below.  My test
browser is IE7 on Windows XP.

Problem 1:
The event handler via the BUTTON element works (1st element within the
form) while using onclick event handler of inline script per <input
type="button" onclick.../> failed.  Why?  How to fix it?

Problem 2:
keyCode of 13 (key down), while it works fine I'd like to exit the
List function if user hit key down twice, what's the keyCode for
pressing key down twice?  If no such keyCode, then how to achieve the
same outcome?

Thanks.

Complete code below
-----------------------------
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
function addList(obj,lt) {
        var theBody = document.getElementById(obj);
        if (lt == 'ol') {var listType = document.createElement('ol')};
        if (lt == 'ul') {var listType = document.createElement('ul')};
        theBody.appendChild(listType);

        var li = document.createElement('li');
        listType.appendChild(li);

        //var keyCode = window.event.keyCode;

        function chkKey() {
          if (event.keyCode == 13) {
                //{alert('carriage return')};

                var newNode = document.createElement('li');
                listType.appendChild(newNode);

                }
        }

        document.onkeydown = chkKey;
     }
</script>

</head>
<body>
<form method="post">
<button onclick="addList('txt','ol')">Order List</button>
<button onclick="addList('txt','ul')">Unordered List</button><br/>
<input type="button" onclick="var theBody =
document.getElementById('txt');var listType =
document.createElement('ol');theBody.appendChild(listType);var li =
document.createElement('li');listType.appendChild(li);function
chkKey() {if (event.keyCode == 13) {var newNode =
document.createElement('li');listType.appendChild(newNode);}}document.onkey­down
= chkKey;}" value="OL"/><br/>
<textarea name="txt" id="txt" cols="60" rows="12"></textarea>
</form>

</body>
</html>

People, is my question statement unclear or ? Please advise. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,861
Messages
2,569,878
Members
46,087
Latest member
KVTRuth63

Latest Threads

Top