www.webdeveloper.com
Results 1 to 7 of 7

Thread: how to input the text in a document?

  1. #1
    Join Date
    Sep 2008
    Posts
    62

    how to input the text in a document?

    Hi guys,

    I am working on my javascript to create the text in the html document. I want to know how I can write the text in html?

    I tried to use this:

    Code:
    var test=myFunction();
    
    function myFunction(test)
    {
      var keycode;
      if (window.event) keycode = window.event.keyCode;
      {
        document.write("hello! this is a test");
      }
    }


    It did not write the text in the html. I tried to find the solution but i can't.

    Can you please help me?

    thanks in advance.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,410
    If you call the function after the page has been rendered,
    the document.write will re-load the page as if you had never been there before.

    Consider using an alert if it is truly only a test.
    Or write to a visible element, like a <div> tag.
    Last edited by JMRKER; 03-26-2013 at 09:42 PM.

  3. #3
    Join Date
    Sep 2008
    Posts
    62
    No, I did not know about the document.write. I never had that score.

    Do I need to create a <span> to create the text in HTML and then enable\disable it using with javascript??

  4. #4
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    One of my early pages (no forms or table) called a function that wrote the time difference as document.write()s. The body contained
    Code:
    It's been
    <SCRIPT language=JavaScript>
    <!--
    compDate("August 29, 2005 09:00");
    //-->
    </SCRIPT>
     since Katrina slammed into Mississippi and Louisiana on Aug. 29, 2005 at about
    9am.
    <br>
    and it's output today (Mar 27, 2013 4pm.) is

    It's been 7 years, 6 months, and 29 days since Katrina slammed into Mississippi and Louisiana on Aug. 29, 2005 at about 9am.

    The whole page is compdate.htm.
    Also have a webpage that writes a narrative on round robin pairings into a text area on a form wyorrp.htm, but that might not be exactly what you need.

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,410
    Quote Originally Posted by mark107 View Post
    No, I did not know about the document.write. I never had that score.

    Do I need to create a <span> to create the text in HTML and then enable\disable it using with javascript??
    I'm not sure what it is that you are trying to accomplish in post #1
    Can you describe your requirement a bit better.

    I believe rthe window,event will only work in MSIE only anyway.

  6. #6
    Join Date
    Sep 2008
    Posts
    62
    well i am trying to create the text using with javascript. however, i am not aware that i need to create the text using with span in html so i created a text. I have got a problem, the text did not appear on my webpage after I wrote this in my javascript:

    Code:
    text1.style.visibility = "visible";

    Here is the span tag I have input in the html page:

    Code:
    <span id="text1"><span style="font-family:Arial;font-size:13px;font-style:normal;font-weight:normal;text-decoration:none;text-transform:none; position:absolute; visibility:hidden; overflow:hidden; left:50px; top:40px; width:376px; height:90px; z-index:0">this is a text </span></span>
    Do you have any idea how to display them in my webpage?

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    Simply use a tag : container (div), paragraph (p) or a span with an id to write

    document.getElementById('myTag').innerHTML='Something to insert in the document';

    Since try this script which give this page :
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <style type="text/css">
    #pge{display:block;width:400px;margin:0 auto;}
    #pge p,input {text-align:center;}
    </style>
    </head>
    <body>
    <div id="pge"><fieldset><legend>How long ?</legend>
    <p>Enter a date (year, month day)
    <br>with separators or leading zéros
    <p><input type="text" id="dte">&nbsp;<a href="Javascript:displayResult()">Ok !</a></p>
    <p id="rsp"></p>
    </div>
     
    <script type="text/javascript">
    function displayResult(){
    	var str=document.getElementById('dte').value, month, date, year;
    	var rgx=/\s*([1-9][0-9]{3})[\D]*(\d|\d\d)[\D]*(\d|\d\d)\s*/;
    	// A replace method with an anonymous function to define year, month and date 
    	str.replace(rgx,
    	// This function arguments are defined by the system 
    	// the matched string and the sub-patterns delimited by brackets (corresponds to $0, $1 $2...) 
    	function(allPat,b1,b2,b3){month=parseInt(b2,10);date=parseInt(b3,10);year=+b1});
    	var rsp=document.getElementById('rsp');
    	if (isNaN(month)) {rsp.innerHTML='Please, enter a valid date with digits, separators or leading zeros (YYYY/M/D or YYYYMMDD)';return}
    	var dat=new Date(year, month-1, date);
    	if (dat.getMonth()!=month-1) {rsp.innerHTML='Sorry, this date seems not valid !';return}
    	else {document.getElementById('rsp').innerHTML='';
    		document.getElementById('dte').value=year+'/'+(month<10?'0':'')+month+'/'+(date<10?'0':'')+date;
    		var tdy=new Date(),deltaYear=deltaMonth=deltaDay=0;
    	 
    	   	 //  For the past
    		if (dat.valueOf()<tdy.valueOf()) {
    			deltaYear=tdy.getFullYear()-year;
    			if (tdy.valueOf()<dat.setFullYear(tdy.getFullYear()).valueOf()) {deltaYear--;}
    			dat.setFullYear(year+deltaYear);
    			while(dat.setMonth(dat.getMonth()+1).valueOf()<tdy.valueOf()) deltaMonth++;
    			dat.setMonth(dat.getMonth()-1);
    			while(dat.setDate(dat.getDate()+1).valueOf()<tdy.valueOf()) deltaDay++;
    		document.getElementById('rsp').innerHTML='It’s been '+deltaYear+' years, '+deltaMonth+' months, and '+deltaDay+' days since this date';			
    		}
    	}
    }		
    </script>
    </body>
    </html>
    I'll let you fill in the dates of future...
    Last edited by 007Julien; 03-29-2013 at 06:26 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles