Solved

Accessing form variable where variable name is a GUID

Posted on 2006-06-14
14
300 Views
Last Modified: 2012-05-05
I am pulling questions out of a databse for an online survey.  The variable names in the html form are GUID's.

for example

<input type="text" name="{cccccccc-cccc-cccc-cccc-cccccccccccc}">

I want to check the form variables with javascript, but when i try and access the variable:

document.formname.{cccccccc-cccc-cccc-cccc-cccccccccccc}.value

I get an error message.

I tried to assign the GUID to a variable as a string:
varname = "{cccccccc-cccc-cccc-cccc-cccccccccccc}"

then access the form variable:
document.formname.varname.value

...but that didn't work either.

Any suggestions?

Thanks in advance
0
Comment
Question by:bizkit112
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 16908911
try use document.getElementById method ? like:

varname = "{cccccccc-cccc-cccc-cccc-cccccccccccc}"

then:

document.getElementById(varname).value ...
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16909003
The name has to be a String because of the special characters.
Then you can use that String in one of two ways:
Either:
document.formname["{cccccccc-cccc-cccc-cccc-cccccccccccc}"].value

Or:
document.formname[varname].value




0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16910536
Another way could be loop over (it is inefficient, but works).

<html>
<head>
<title>Form Field Eval</title>
</head>
<body>
<script language="javascript">
function catchErr(err) {
      alert (err);
}
onerror = catchErr;
function GetValue (form, fldName) {
  for (ix=0; ix < form.elements.length; ix++) {
     if (form.elements[ix].name.match(fldName)) {
      alert (form.elements[ix].value);
      return (form.elements[ix].value);
     }
  }
 
}
</script>
<form name="form1">
<input type="text" name="{cccccccc-cccc-cccc-cccc-cccccccccccc}">
<input type="button" value="Get Value of GUID"
onclick='GetValue(this.form,"{cccccccc-cccc-cccc-cccc-cccccccccccc}");'>
</form>
</body>
</html>
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 63

Expert Comment

by:Zvonko
ID: 16910982
Sorry, but what your GetValue() does is identical to: this.form["{cccccccc-cccc-cccc-cccc-cccccccccccc}"].value;
So why the error-prone loop? Don't you know that match() does also match a subset?
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16911976
(form.elements[ix].name == fldName) does the exact match check.
Thanks Zvonko for helping me point out a possible flaw in my code.



<html>
<head>
<title>Form Field Eval</title>
</head>
<body>
<script language="javascript">
function catchErr(err) {
     alert (err);
}
onerror = catchErr;
function GetValue (form, fldName) {
  for (ix=0; ix < form.elements.length; ix++) {
     if (form.elements[ix].name == fldName) {
     alert (form.elements[ix].value);
     return (form.elements[ix].value);
     }
  }
 
}
</script>
<form name="form1">
<input type="text" name="{cccccccc-cccc-cccc-cccc-ccccccccccccccc}">
<input type="text" name="{cccccccc-cccc-cccc-cccc-cccccccccccc}">
<input type="button" value="Get Value of GUID"
onclick='GetValue(this.form,"{cccccccc-cccc-cccc-cccc-cccccccccccc}");'>
</form>
</body>
</html>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16912040
Flaw or not flaw, the loop is NOT necessary.
0
 

Author Comment

by:bizkit112
ID: 16917690
Here are the results I had...

when i use the following code:

varname = "{cccccccc-cccc-cccc-cccc-cccccccccccc}"
if (document.healthhistoryform.getElementByID(varname).value == '')

i get error: Object doesn't support this property or method

when i use:

if (document.healthhistoryform["{cccccccc-cccc-cccc-cccc-cccccccccccc}"].value == '')

i get the error:  '{GUID}.value' is null or not an object

and i know the form variable is not null.

Thanks for your help so far, are there any other suggestions?

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16918055
Do you have the correct UpperCase and LowerCase spelling in healthhistoryform name and {cccccccc-cccc-cccc-cccc-cccccccccccc} field name? Best copied by copy'n paste.

0
 

Author Comment

by:bizkit112
ID: 16947660
Yes, the case is correct.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16948600
OK, the form element does not support the getElementByID() method, and even when you correct would call the document.getElementById() you cannot get a form element name when it has not the same id as name.
Can wee see your page source or at least the concerned parts? Or is your test page online?

0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16982968
I have tried my post ???

0
 

Author Comment

by:bizkit112
ID: 17066769
<script>
<!--

function checkForm()
{
      var validForm = false;
      alert("here");
      varname = "{66cd89ce-4326-43b9-a9ac-f3c5858901cc}"
      if (document.getElementByID(varname).value == '')
      {
            validForm = false;
            alert("it worked");
      }
      alert("after");
}


// -->
</script>

<form name="healthhistoryform" method="post" onsubmit="return checkForm()" action="#">
<table width="760" border="0" cellpadding="0" cellspacing="0">
      <tr>
            <td width="25"><%=i%></td>
            <td width="250"><%=HealthHistory("Question")%></td>
            <td><input type="<%=HealthHistory("Type")%>" name="<%=HealthHistory("GUID")%>" id="<%=HealthHistory("GUID")%>"></td>
      </tr>
</table>
</form>


Here, the GUID in the javascript function matches the HealthHistoryForm("GUID") value in the input field.
It did not work with the getElementByID, as mentioned before, possibly b/c it has the same value for name as it does for id.

I get the following error:
     Object doesn't support htis property or method

on this line of code:
     if (document.getElementByID(varname).value == '')
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 17066798
Javascript is case sensitive, try use document.getElementById instead of document.getElementByID, like:


function checkForm()
{
     var validForm = false;
     alert("here");
     varname = "{66cd89ce-4326-43b9-a9ac-f3c5858901cc}"
     if (document.getElementById(varname).value == '')
     {
          validForm = false;
          alert("it worked");
     }
     alert("after");
}
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 17066801
ah, this is same as what Zvonko already mentioned above.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question