Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Javascript writing correctly but not firing event 1

Status
Not open for further replies.

trunkrecords

Programmer
Jul 3, 2007
4
US
Hello all,

I am writing an application which dynamically generates forms based upon info contained within a MySql db. The problem that I am having is this:

I have 2 loops to write a checkbox and a textbox for a particular type of question. Part of the form behaves normally and part does not. Here is the code which operates correctly -
Code:
<input name='c1414' type='checkbox' value='Discharge diagnosis (choose as many as apply from the list below)'
 onclick="javascript:if (this.checked){ document.questionqueryform.txt1414_1.disabled=false; }"
 onmouseup="javascript:if (this.checked){ document.questionqueryform.txt1414_1.disabled=true; }"  />
&nbsp;&nbsp;Other, Specify&nbsp;&nbsp;&nbsp;
<input name='txt1414_1' type='text' value='' id='txt1414_1' class='' disabled=true />

This code does not fire correctly -
Code:
<input name='c1417' type='checkbox' value='Limb reduction defects' 
onclick="javascript:if (this.checked){ document.questionqueryform.txt1417_1.disabled=false; }" 
onmouseup="javascript:if (this.checked){ document.questionqueryform.txt1417_1.disabled=true; }"  />
&nbsp;&nbsp;Limb reduction defects&nbsp;&nbsp;&nbsp;
<input name='txt1417_1' type='text' value='' id='txt1417_1' disabled=true />

Does anyone see the problem? To me the code looks exactly the same except for the names.
 
Trunk, you code works fine for me! Are you getting a javascript error?

Also, wasn't quite sure why you used onclick and mouseup... this has the same effect:

Code:
<input name='c1414' type='checkbox' value='Discharge diagnosis (choose as many as apply from the list below)'
 onclick="javascript:if (this.checked){ document.questionqueryform.txt1414_1.disabled=false;}else{document.questionqueryform.txt1414_1.disabled=true; }" />

Let us know the error if your getting one and hopefully we'll be able to resolve for you.

Cheers

Nick
 
Thanks Nick.

Well that is the curious thing... there isn't an error. The code should enable and disable the named textbox. It works for some of the textboxes but not all.

Good call on the mousedown. I'll try the else condition and see if that works.

 
Changed the code to -

Code:
<input name='c1417' type='checkbox' value='Limb reduction defects' onclick="javascript:if (this.checked){ document.questionqueryform.txt1417_1.disabled=false; }else{ document.questionqueryform.txt1417_1.disabled=true; }"  />&nbsp;&nbsp;Limb reduction defects&nbsp;&nbsp;&nbsp;<input name='txt1417_1' type='text' value='' id='txt1417_1' disabled=true />

but still no error and no enable of the textbox.
 
Have you checked for a conflicting element ID/Name on the page?
 
The code does look the same... so in isolation we are not going to resolve the problem. You should attempt to validate your page once it has rendered - this will identify malformed markup (which could cause this problem) as well as identify duplicated IDs (another possible cause for this problem).

Some observations...

You do not need to use javascript: in an onclick... so you can remove all those for a start.

Given you are using IDs for your form elements, I suggest you change your syntax to accomodate them. Given the naming scheme you use, you can use the following so that all onclicks are the same:
Code:
onclick="if(this.checked){document.getElementById('txt' +this.name.substring(1)+ '_1').disabled='false';}else{document.getElementById('txt' +this.name.substring(1)+ '_1').disabled='true';}"

Cheers,
Jeff


[tt]Jeff's Page [!]@[/!] Code Couch
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
nickdel - good catch.

There is a hidden field written at the same time (it passes a question identifier to the code behind for server validations) that I had not given an unique name.

Works like a charm!

Thanks guys for helping me see this problem clearly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top