• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 22086
  • Last Modified:

vbScript getElementById

ASP3, trying to access an element id:

Here is the element:
<input type="TEXT" size="36" name="Contact Name" id="210">

And I am trying to do something simple in VBSCRIPT:

dim el
dim id
id=210
el = document.getElementById(id)
el.value="Some Value"


but it blows up on the getElementById line?

Any suggestions ??
0
lrr81765
Asked:
lrr81765
  • 6
  • 2
  • 2
  • +1
2 Solutions
 
deighcCommented:
It could be that getElementById expects a string value and you're passing in an integer. Try forcing the value to a string:

el = document.getElementById("" & id)

or

el = document.getElementById(cstr(id))
0
 
fritz_the_blankCommented:
The VBScript DOM doesn't work that way. As far as I know, you have to use the names like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script language="VBScript">
sub showMe()
      set el=Document.frmForm1.Elements("210")
      el.value="Some Value"
      msgbox el.value
end sub
</script>
</head>
<body onLoad="showMe()">
<form method=post action="" id="frmForm1" name="frmForm1">
      <input type="TEXT" size="36" name="210" id="210">
</form>
</body>
</html>
0
 
fritz_the_blankCommented:
I suppose to be more accurate, something like this?

set el=Document.forms("frmForm1").Elements("210")
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fritz_the_blankCommented:
Oops, I was wrong--you can do it with ID tags like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script language="VBScript">
sub showMe()
      set el=Document.forms("frmForm1").Elements("210")
      el.value="Some Value"
      msgbox el.value
end sub
</script>
</head>
<body onLoad="showMe()">
<form method=post action="" id="frmForm1">
      <input type="TEXT" size="36"  id="210">
</form>
</body>
</html>
0
 
fritz_the_blankCommented:
It appears that the ID tags are interrogated first and then the name tags as this example shows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script language="VBScript">
sub showMe()
      set el=Document.forms("frmForm1").Elements("210")
      el.value="Some Value"
      msgbox el.value
end sub
</script>
</head>
<body onLoad="showMe()">
<form method=post action="" id="frmForm1" name="NotfrmForm1">
      <input type="TEXT" size="36"  id="210" name="Not210">
</form>
</body>
</html>
0
 
fritz_the_blankCommented:
It's official--I am a moron!

I don't deal with client-side VBScript much, so I was half coding in JavaScript. Here is how you can use the .getElementByID method in VBScript:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script language="VBScript">
sub showMe()
      set el=Document.getElementByID("210")
      el.value="Some Value"
      msgbox el.value
end sub
</script>
</head>
<body onLoad="showMe()">
<form method=post action="" id="frmForm1" name="NotfrmForm1">
      <input type="TEXT" size="36"  id="210" name="Not210">
</form>
</body>
</html>
0
 
deighcCommented:
For the record...

getElementById() is a method of the document object. This has nothing to do with Javascript or VBScript. It comes from the browser DOM.

But as FTB correctly pointed out whenever you're assigning an object to a variable in VBScript you must use the 'set' keyword. The methodology is the same regardless of the scripting language but the syntax changes slightly.
0
 
fritz_the_blankCommented:
@deighc --

I suppose that the higher points in the split went to you as the issue of the delimiters was more important than the syntax.

FtB
0
 
sysbezeqintCommented:
only works on IE, any idea for opera / firefox ?
0
 
NuPay_DevCommented:
I did this way to initiate a value instead. This was for a report and not really meant for dynamic code.

1) declare vbscript variable.
2) insert value directly into the input control.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script language="VBScript">
      el_value="Some Value"
</script>
</head>
<body>
<form method=post action="" id="frmForm1" name="NotfrmForm1">
 
      <input type="TEXT" size="36"  id="210" name="<% el_value %>">
 
</form>
</body>
</html>

Open in new window

0
 
NuPay_DevCommented:
Sorry, I meant to say
<input type="TEXT" size="36"  id="210" name="Not210" value="<% el_value %>">

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now