Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    60
    Thanks
    6
    Thanked 6 Times in 6 Posts

    Question Generator Issues

    I am trying to make a link generator so far i have this...
    Code:
    <script>
    function cod(url, txt, target) {
    if (target==blank) {
    var trgt="target=\"_blank\"";
    }
    else {
    trgt="";
    document.getElementById('code').innerHTML = "<a href=\"" + url + "\"" + trgt + ">;" + 
    
    txt + "</a>"
    }
    </script>
    URL<input type="text" id="url"><br>Link Text<input type="text" id="txt"><br>New Window<input type="checkbox" 
    
    id="target" value="blank"><br><button onclick="cod(document.getElementById('url').value, 
    
    document.getElementById('txt').value, document.getElementById('target').value);">Go!
    
    </button><br><textarea id="code"></textarea>
    But nothing happens when the function is called does anyone know why?
    Last edited by uncrocks; 08-26-2008 at 12:10 AM.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    • First, you miss to close your cod function.
    • This part is erroneous:
      Code:
      if (target==blank)
      You need to surround it with quotes:
      Code:
      if (target=='blank')
      But that does'nt makes sense. Since the checkbox value will always be "blank", not unless you explicitly change it. You must verify if the checkbox is checked instead:
      Code:
      if (document.getElementById('target').checked)
    • Use value in replace of innerHTML for textarea.
    • Your script logic is erroneous. If checkbox is checked, you'll assign to trgt variable the value target="_blank", otherwise print to the textarea the code.


    You might find this modification useful:
    Code:
    function cod(url, txt, target) {
    var trgt="";
    if (document.getElementById('target').checked) trgt="target=\"_blank\"";
    document.getElementById('code').value = "<a href=\"" + url + "\" " + trgt + ">" + txt + "</a>"
    }
    Hope it helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

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