Results 1 to 5 of 5

Thread: Dynamic Page Help

  1. #1
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dynamic Page Help

    I am a javascript newbie. I am accepting data in a form. I need to display a line in the table asking for the address of the link, but only if one exists. Here is how I am trying to find out if a link exists:

    HTML Code:
     <td>Is the ad linked to a web page? </td>
       <td>Yes
        <input name="adlinked" type="radio" value="Yes" onclick= "getlink(adlinked)">
        No
        <input name="adlinked" type="radio" value="No" /></td>
        </tr>
    Here is the function:


    Code:
    <SCRIPT LANGUAGE="JavaScript1.2">
    function getlink(linked)
    {
    	if (linked == "Yes")
    	document.write ("<tr><td>Address of web page:</td> . <td><input name="thelink" type="text" id="thelink" size="50" /></td></tr>")
    }
    </script>
    Nothing happens. Please help

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <input name="adlinked" type="radio" value="Yes" onclick="getlink('Yes')">
    Code:
    function getlink(linked)
    {
    	if (linked == "Yes")
    	document.write ('<tr><td>Address of web page:</td> . <td><input name="thelink" type="text" id="thelink" size="50" /></td></tr>');
    }
    Now something will happen, but you probably won't like it.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default You're Right Don't Like it

    John,

    Your right I don't like it. What do I need to have that line added to what is on the page currently and not just creat a new page?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's the problem with the document.write() method. If it's used after the page is loaded, it overwrites the page. Generally the easiest (though not really the best) way to add to an existing page is to have an element waiting and either add to its innerHTML property or overwrite its innerHTML property. The fact that this is a table, complicates things, as tables are finicky to work with and have a complex structure that must be observed properly when making changes to it. Otherwise you get garbage. I'll write up something that should work, at least for a few basic things.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Here's a simple demo:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function getlink(){
    	if(document.getElementById('thelink')) {return;}
    	if(getlink.tr){
    		document.getElementById('targetBody').appendChild(getlink.tr);
    		return;
    	}
    	var tb = document.getElementById('targetBody'), tr = document.createElement('tr'),
    	td1 = document.createElement('td'), td2 = td1.cloneNode(false);
    	td1.innerHTML = 'Address of web page:';
    	td2.innerHTML = '<input name="thelink" type="text" id="thelink" size="50">';
    	tr.appendChild(td1);
    	tr.appendChild(td2);
    	tb.appendChild(tr);
    	getlink.tr = tr;
    }
    function killlink(){
    	if(!document.getElementById('thelink')) {return;}
    	document.getElementById('targetBody').removeChild(getlink.tr);
    }
    </script>
    </head>
    <body>
    <table>
    <tbody id="targetBody">
    <tr>
    <td>Is the ad linked to a web page? </td>
       <td>Yes
        <input name="adlinked" type="radio" value="Yes" onclick="getlink();">
        No
        <input name="adlinked" type="radio" value="No" onclick="killlink();"></td>
        </tr>
    </tbody>
    </table>
    
    </body>
    </html>
    If a form is involved, as appears to be your intention, things may get whacky (as far as what happens when the form submits). But it might not, you will have to test that out to see.
    Last edited by jscheuer1; 11-01-2009 at 06:30 PM. Reason: minor code imrovement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •