SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Smolf3d's Avatar
    Join Date
    Aug 2004
    Location
    russia
    Posts
    232
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Copy input text without submit button

    here is the code,with submit button

    Code HTML4Strict:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
    <title>New Page 1</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    function doIt()
    {
        writit(document.forms[0].text.value,'testwrite');
    }
     
    function writit(text,id)
    {
        if (document.getElementById)
        {
            x = document.getElementById(id);
            x.innerHTML = '';
            x.innerHTML = text;
        }
        else if (document.all)
        {
            x = document.all[id];
            x.innerHTML = text;
        }
     
    }
    // -->
    </script>
    </head>
    <body>
    <form onsubmit="writit(this.text.value, 'testwrite'); return false;">
    <input size="20" name="text">
    <input type="submit" value="Write text">
    </form>
    <div id="testwrite" name="testwrite"></div>
    </body>
    </html>

    How to do without ?

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly what do you want to achieve? Also... there is no need for having an if/else in that code, since IE also supports getElementById

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can add event binding onto any element you want using the addEventListener method (attachEvent in IE). I've tidied this up for you to an add event function.

    Code:
    var addEvent = function(){
    	if (window.addEventListener){
    	    return function(el, evtype, fn) {
    			el.addEventListener(evtype, fn, false); 
    		};
    	}else if (window.attachEvent){
    		return function(el, evtype, fn) {
    			el.attachEvent("on"+evtype, fn);
    		};
    	}
    }();
    el is the element, evtype the event (eg 'click') and fn the function.

    Code:
     
    <input size="20" name="text" id="text"> 
    <script>
    elem = document.getElementById('text');
    addEvent(elem, 'change', function(){alert("yay";});
    </script>
    Mashups, DOM Scripting and all the things we call Web2.0 blogger
    http://www.heavyweightgeek.org/
    http://twitter.com/heavyweightgeek


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •