473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

110 100+
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_State". 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?

Expand|Select|Wrap|Line Numbers
  1.     <a href="mailto:amandasl@ub.com?subject=I need surplus lines tax forms to bind a Truckers GL submission in "+'javascript:document.getElementById("Garaging_State").value'+".">
  2.  
Feb 4 '09 #1
9 11996
Dormilich
8,658 Expert Mod 8TB
@andersond
of course, the closing quotation mark appears there.

better set the value onload.

Expand|Select|Wrap|Line Numbers
  1. <a id="sendmail" href="mailto:user@host.tld?subject=I need...ssion in ">...</a>
  2. // JS
  3. function setSubject()
  4. {
  5.   var state = document.getElementById("Garaging_State").value;
  6.   var link = document.getElementById("sendmail");
  7. // using DOM
  8.   var href = link.getAttribute("href");
  9.   link.setAttribute("href", href+state);
  10. // or maybe even this works
  11.   link.href += state;
  12. }
  13.  
  14. window.addEventListener("load", setSubject, false);
something like that...
Feb 4 '09 #2
andersond
110 100+
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.

Expand|Select|Wrap|Line Numbers
  1. <a id="sendmail" href="javascript:emailAmanda()">
  2.  
  3. function emailAmanda(){
  4.     var garagingState = document.getElementById('Garaging_State').value;
  5.     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;
  6.     var href = link.getAttribute("href"); 
  7.     link.setAttribute("href", message2Amanda);
  8. }
  9.  
Please show me how to make this work.
Feb 9 '09 #3
acoder
16,027 Expert Mod 8TB
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.
Feb 10 '09 #4
andersond
110 100+
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.
Feb 10 '09 #5
acoder
16,027 Expert Mod 8TB
By closing this link, I mean this line:
Expand|Select|Wrap|Line Numbers
  1. <a id="sendmail" href="javascript:emailAmanda()">
it's missing the link text and closing </a> tag, e.g.
Expand|Select|Wrap|Line Numbers
  1. <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:
Expand|Select|Wrap|Line Numbers
  1. var link = document.getElementById("sendmail");
Feb 10 '09 #6
andersond
110 100+
Something is still missing. FireFox (FireBug) error says "link" has no properties. What does that mean as it applies to this case?
Feb 10 '09 #7
acoder
16,027 Expert Mod 8TB
Have you added the "var link.." line before line 6? Can you show the latest version of your code?
Feb 10 '09 #8
andersond
110 100+
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.

Expand|Select|Wrap|Line Numbers
  1. <a id="sendmail" href="javascript:emailAmanda()">click here</a>
  2.  
  3. function emailAmanda(){ 
  4.     var message2Amanda = "mailto:amandas@ubinc.com?subject=I need surplus lines tax forms to bind a Truckers GL submission on "+applicantName;  
  5.     var href = link.getAttribute("href"); 
  6.     var link = document.getElementById("sendmail");  
  7.     link.setAttribute("href", message2Amanda); 
  8. }
  9.  
Feb 10 '09 #9
acoder
16,027 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function ...
  3. ...
  4. }
  5. </script>
Feb 10 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: IkBenHet | last post by:
Hello, I found this script to create a simple rich text form (http://programmabilities.com/xml/index.php?id=17): <html> <head> <title>Rich Text Editor</title> </head> <body>
1
by: swethak | last post by:
hi, i have a code to disply the calendar and add events to that. It works fine.But my requirement is to i have to disply a weekly and daily calendar.Any body plz suggest that what modifications i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.