Solved

Updating a html ID with a document.all. htmlID .value via a javascript

Posted on 2006-12-01
8
319 Views
Last Modified: 2013-12-16

i am updating a html ID with a document.all.htmlID.value via a javascript.
i can hard code the htmlID and update the value but in this case the html id is dynamic and changes for variables.  

here is the scenario...
note: document.all.html_tag is a variable, and document.all.Price235
this doesnt work...
====================================
var html_tag = "Price" + items[itemCount].getAttribute("ID");
//html_tag = "Price235"
if (document.all.html_tag.value)
      document.all.html_tag.value = this.GetFormattedAttributeValue(items[itemCount], "Price");
                  
this works and updates my webpage buts its hardcoded.
====================================
var html_tag = "Price" + items[itemCount].getAttribute("ID");
if (document.all.html_tag.value)
      document.all.Price235.value = this.GetFormattedAttributeValue(items[itemCount], "Price");
0
Comment
Question by:mploschiavo
8 Comments
 
LVL 6

Expert Comment

by:davbouchard
Comment Utility
Have you tried using
document.getElementById("Price" + items[itemCount].getAttribute("ID")).value = this.GetFormattedAttributeValue(items[itemCount], "Price");
0
 
LVL 2

Author Comment

by:mploschiavo
Comment Utility
i cant seem to get that to work... i get a
error: document.getElementById(...) is null or not an object
0
 
LVL 2

Author Comment

by:mploschiavo
Comment Utility
this doesnt work either....  

eval("document.all."+html_tag+".value") = this.GetFormattedAttributeValue(items[itemCount], "Price");
0
 
LVL 18

Expert Comment

by:Morcalavin
Comment Utility
Try
document.all[html_tag].value
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Accepted Solution

by:
gprina earned 500 total points
Comment Utility
Well... it's a really old syntax, and it doesn't work for me in the latest versions (I don't know which version of IE or Firefox you are using), but if the second portion of code (the one hardcoded) it's working for you please try this:

eval("document.all."+html_tag).value = this.GetFormattedAttributeValue(items[itemCount], "Price");

I just placed the "value" outside the eval, because you just need to obtain the object.

Double check that you also change the previous IF and also check that html_tag contains the right value.
0
 
LVL 2

Author Comment

by:mploschiavo
Comment Utility
thanks gprina
i got it to work .... works on ie6...  since its old syntax... is there a better way?

if (items[itemCount].getAttribute("ID"))
                  {      if ( eval("document.all."+"Price" + items[itemCount].getAttribute("ID")) != null )
                              eval("document.all."+"Price" + items[itemCount].getAttribute("ID")).value = this.GetFormattedAttributeValue(items[itemCount], "Price");

0
 
LVL 1

Expert Comment

by:gprina
Comment Utility
Check this out:

if (items[itemCount].getAttribute("ID"))
               {
                 var obj = eval("document.all."+"Price" + items[itemCount].getAttribute("ID"));
                 if ( obj  != null )
                         obj.value = this.GetFormattedAttributeValue(items[itemCount], "Price");
0
 
LVL 1

Expert Comment

by:gprina
Comment Utility
Ops... I may be missinterpreted your last question, but if

  document.getElementById(html_tag).value = this.GetFormattedAttributeValue(items[itemCount], "Price");

no, I think there is no a better syntax for IE6
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Read about why website design really matters in today's demanding market.
The viewer will learn how to dynamically set the form action using jQuery.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now