473,405 Members | 2,176 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,405 software developers and data experts.

Parsing for 2+ attributes of the same name in JavaScript

Hi, this request is sort of 2 questions in 1 but both are related.

Is there a way to parse the HTML of a page, after a user has made changes to the controls on that page (e.g. checking a checkbox that was originally un-checked, or typing text in a text box, etc.), using innerHtml??

I know that using the document.getElementById is only able to return the attributes of a page on load but that it cannot deal with changes to control values once it has loaded.

I have been able to parse the html using innerHtml OK and I know that the .search() method is able to cater for finding the first appearance of a string.

However, I have several checkbox controls on the same page, whose attribute I need to retrieve, in the most efficient way.

Is anyone familiar with this issue and can give me advice on how to do that>?

Many Thanks
Matt
Aug 30 '12 #1

✓ answered by Rabbit

You will see from line 7 of gits post that the value of a checked checkbox is not equal to the string "checked". Rather, the value of a checkbox is stored in the checked property of the object.

So yes, your first question, as it were, in post #4 is answered with yes, you care incorrectly retrieving the value of the checkbox, hence the impression that it is not being updated according to user input.

6 1917
Rabbit
12,516 Expert Mod 8TB
I know that using the document.getElementById is only able to return the attributes of a page on load but that it cannot deal with changes to control values once it has loaded.
You have that backwards. document.getElementById is definitely the way to get the value as it currently stands. The innerHTML attribute does not change based on the control's current value. I'm not sure what code you were using but what you have stated is the opposite of what happens.
Aug 30 '12 #2
gits
5,390 Expert Mod 4TB
the efficiency of such a code depends a bit on the page and its dynamics, which means can the number of elements to check vary a lot or are there different elements depending on user-interactions etc.

it would help to see some code to give some advice if you have issues with your code.
Aug 31 '12 #3
Hi Rabbit & gits, thankyou for your prompt reply. Thanks!

OK so Rabbit, you say that getElementById picks up the current state of the controls on the webpage. Well, what I am attempting to do is test the value of the "checked" attribute on a checkbox and run the method that contains this logic, from a button on my webpage.

The testing logic method for the checkbox in my .js file is:

Expand|Select|Wrap|Line Numbers
  1. function TestInpatientBenefitsTry2() {
  2.  
  3.     // test that we are calling this correctly
  4.     alert('Test run started');
  5.  
  6.     var inpatientHarmony5M = "12644_HasBenefit_0";
  7.     var myObjHarmony5M = txt.getElementById(inpatientHarmony5M).attributes("checked");
  8.  
  9.     var inpatientHarmony2M = "12581_HasBenefit_1";
  10.     var myObjHarmony2M = txt.getElementById(inpatientHarmony2M).attributes("checked");
  11.  
  12.     if (myObjHarmony5M == "checked") {
  13.         alert('Harmony 2M is disabled \n because Harmony 5M is selected');
  14.     }
  15.  
  16.     if (myObjHarmony2M == "checked") {
  17.         alert('Harmony 5M is disabled \n because Harmony 2M is selected');
  18.     }
  19.  
  20.     if (myObjHarmony5M != "checked" || myObjHarmony2M != "checked") {
  21.             alert('No outpatient benefits are enabled because no inpatient benefit has been selected');
  22.         }
  23.  
  24.         alert("Test Run Completed");
  25. }
  26.  
And, in case you want to see it, in my vbhtml web page, I call it like so:

Expand|Select|Wrap|Line Numbers
  1.     @<input type="button" value="Run Tests" onclick='RunTests()' />
  2.  
  3. function RunTests()
  4.     {
  5.         TestInpatientBenefitsTry2();
  6.     }
  7.  
The test still isn't working as expected - if I have the inpatientHarmony5M box checked then, I would expect to see the alert box; 'Harmony 2M is disabled \n because Harmony 5M is selected' and similarly, I would expect to see the second alert box in my code passage, for the Harmony 5M checkbox.

If I take out the 2nd and 3rd if statement, and have the Harmony 2M checkboxes checked, the relevant checkbox is displayed. However, if I put all three if statements in the function to check the attributes of the checkboxes given the 3 possible scenarios, then none of the messages are displaying.

So I would like to know 2 things:

1. Am I calling and checking the attribute values of the checkboxes correctly in the condition of my 'if' statements?
2. Have I structured my if statements correctly or am I missing some logic?

Thanks again
Matt
Aug 31 '12 #4
I've partially resolved the problem as I noticed that I should have been calling the document element to interact with the getElementById operation rather than the txt variable that I had created myself.

However, the correct message doesn't show whether you change the value of the check box on the page and the 3rd if statement, with the != comparisons too look for the possibility that either checkbox is disabled, shows every time.

So it still appears that the latest state of the page is not picked up when the user changes their options!
Aug 31 '12 #5
gits
5,390 Expert Mod 4TB
yea - the problem is the wrong basereference for the getElementById-method which is a method of the document-object. a simple basic example to work with is this:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <script type="text/javascript">
  3.  
  4. function run_test() {
  5.     var id = 'foo';
  6.  
  7.     var test = document.getElementById('foo').checked;
  8.  
  9.     alert(test);
  10. }
  11.  
  12. </script>
  13. <body onload="run_test();">
  14.     <form name="myform"  >
  15.         <input type="checkbox" id="foo">foobar-text</input>
  16.         <input type="checkbox" id="bar">bar-text</input>
  17.         <input type="button" onclick="run_test();" value="Run Test again"/>
  18.     </form>
  19. </body>
  20. </html>
  21.  
glad to hear you already solved your issue.
Aug 31 '12 #6
Rabbit
12,516 Expert Mod 8TB
You will see from line 7 of gits post that the value of a checked checkbox is not equal to the string "checked". Rather, the value of a checkbox is stored in the checked property of the object.

So yes, your first question, as it were, in post #4 is answered with yes, you care incorrectly retrieving the value of the checkbox, hence the impression that it is not being updated according to user input.
Aug 31 '12 #7

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

Similar topics

1
by: anoop | last post by:
how can I call javascript function by adding button's attribute such as "onclick" in the code behind file as ".aspx.vb" I have written code as Draw_line.Attributes("Onclick")="javascript: where...
1
by: Mantorok | last post by:
Hi all I'm adding some controls in Page_Load and want to add an attribute: control.Attributes = "test"; However, can I access this attribute from javascript? Thanks Kev
3
by: Jeremy Chapman | last post by:
I've created a custom control which outputs html at runtime like <span id="controlid" value="myvalue"></span>. On my page I have javascript that changes the value of the value attribute. On post...
11
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or...
1
by: LG | last post by:
I want to add attributes to a custom dropdownlist and I found some code on Internet that doesn't work after post back. First time the control has the attributes, but after postback the attributes...
2
by: P4trykx | last post by:
Hello I'm want to add some custom attributes to WebControls using WebControl.Attributes.Add("abc","234"); So the html output will look like this, <input type="hidden" abc="123" /etc. I know...
2
by: Safalra | last post by:
Recently I've rewritten much of my old Javascript code to use DOM functions so that enhancements can be attached to documents without needing to alter the HTML code. I assumed that adding event...
5
by: 1965 | last post by:
I am always confused about html's variable expression with ". I know below an error, but I do not know how to fix it. This is a short one, it does not open in new window: Please help: var ref="<a...
5
by: vaibhav03 | last post by:
Hello All, I have made an AJAX call and bringing back some data from the server and then That data would be converted to XML through javascript. Now i am creating various elements like TABLE, TR,...
4
by: rjvrnjn | last post by:
I have a XML string returned from SQL server as a result of select query. The XML string looks like: <row col1="value1", col2="value2"/><row col1="value12", col2="value22"/> At the client side...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.