How can I insert a javascript field value into href line in HTML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andersond
    New Member
    • Feb 2007
    • 110

    How can I insert a javascript field value into href line in HTML

    I have a page that has a hot link to email an underwriter to request tax forms. Earlier, on the same page, the user selects a state in a drop-down box called "Garaginjg_Stat e". Because the tax forms needed may come from any state in the union the underwriter needs to know which state is needed. So, I want to insert the state name in the subject line of the email being sent to the underwriter. The code line below didn't work. The subject line was truncated after the words "submission in". What do I need to change to make this work?

    Code:
        <a href="[EMAIL="amandasl@ub.com?subject=I"]mailto:amandasl@ub.com?subject=I[/EMAIL] need surplus lines tax forms to bind a Truckers GL submission in "+'Javascript:document.getElementById("Garaging_State").value'+".">
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by andersond
    The subject line was truncated after the words "submission in".
    of course, the closing quotation mark appears there.

    better set the value onload.

    Code:
    <a id="sendmail" href="mailto:user@host.tld?subject=I need...ssion in ">...</a>
    // JS
    function setSubject()
    {
      var state = document.getElementById("Garaging_State").value;
      var link = document.getElementById("sendmail");
    // using DOM
      var href = link.getAttribute("href");
      link.setAttribute("href", href+state);
    // or maybe even this works
      link.href += state;
    }
    
    window.addEventListener("load", setSubject, false);
    something like that...

    Comment

    • andersond
      New Member
      • Feb 2007
      • 110

      #3
      adding a variable to the subject line of an email

      Because things in insurance are picky and because every state does the same thing differently, we need to determine which state is important to a particular request. So, having determined the name of the state in question (Garaging_State ) I need to insert the name of the state into the subject line of an email. This seems so simple; but, I just can't get my mind wrapped around it.

      In other words, if the Garaging_State is Indiana, the subject line of the email should read: "I need surplus lines tax forms to bind a Truckers GL submission in Indiana"

      The following code doesn't work.

      Code:
      <a id="sendmail" href="javascript:emailAmanda()">
      
      function emailAmanda(){
      	var garagingState = document.getElementById('Garaging_State').value;
      	var message2Amanda = "mailto:amandas@ubinc.com?subject=I need surplus lines tax forms to bind a Truckers GL submission in "+document.getElementById('Garaging_State').value;
      	var href = link.getAttribute("href"); 
      	link.setAttribute("href", message2Amanda);
      }
      Please show me how to make this work.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You haven't closed the link and 'link' in the function is undefined because you haven't set it to the link element.

        PS. merged duplicate threads.

        Comment

        • andersond
          New Member
          • Feb 2007
          • 110

          #5
          Thank you for merging the duplicate threads. I started a new thread because I didn't feel like I was getting anywhere with this one. I appreciate the fact that someone has responded; but, I really wish I could understand the response. I'm not sure which is more frustrating.

          Now, since I obviously don't know what is wrong or how to fix what is wrong then I also don't understand this response. "You haven't closed the link and 'link' in the function is undefined because you haven't set it to the link element." Please explain how to close the link and set it to the link element, whatever that is.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            By closing this link, I mean this line:
            Code:
            <a id="sendmail" href="javascript:emailAmanda()">
            it's missing the link text and closing </a> tag, e.g.
            Code:
            <a id="sendmail" href="javascript:emailAmanda()">Email Amanda</a>
            Also, note that if the code is exactly how you've posted it, that would also cause problems because JavaScript should be in <script> tags.

            By 'link', I mean line 7. It's not set anywhere with the var keyword as you have set variables on the previous lines. You can set it to the link with:
            Code:
            var link = document.getElementById("sendmail");

            Comment

            • andersond
              New Member
              • Feb 2007
              • 110

              #7
              Something is still missing. FireFox (FireBug) error says "link" has no properties. What does that mean as it applies to this case?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Have you added the "var link.." line before line 6? Can you show the latest version of your code?

                Comment

                • andersond
                  New Member
                  • Feb 2007
                  • 110

                  #9
                  Because FF balked at the other variable lines I dropped them and substituted a global variable, "applicantName" , defined at the beginning of the script. It refers to the same data. Here's the latest code.

                  Code:
                  <a id="sendmail" href="javascript:emailAmanda()">click here</a>
                  
                  function emailAmanda(){ 
                      var message2Amanda = "mailto:amandas@ubinc.com?subject=I need surplus lines tax forms to bind a Truckers GL submission on "+applicantName;  
                      var href = link.getAttribute("href"); 
                      var link = document.getElementById("sendmail");  
                      link.setAttribute("href", message2Amanda); 
                  }

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Note that in line 5, you're using the link variable, but it's defined on the next line. In any case, you don't need that line (5), so you can just remove it.

                    One other thing: make sure the function is in script tags:
                    Code:
                    <script type="text/javascript">
                    function ...
                    ...
                    }
                    </script>

                    Comment

                    Working...