+ Start a Discussion
andyeandye 

Javascript set text box value

Hi all,

 

I've got an issue which is driving my absolutely crazy and it seems like a really stupid problem! I simply want to populate the value of an inputText. Now, here's the strage part:

 

This works:

 

 

function checkLicenses() { var licInputs = document.getElementsByName('license'); var licTextBox = document.getElementById('{!$Component.licenseAssignments.licenseWiz2Form.txtSelectedLicenses}'); licTextBox.value = licInputs.length; }

 

This also works:

 

 

function checkLicenses() { var licInputs = document.getElementsByName('license'); var licTextBox = document.getElementById('{!$Component.licenseAssignments.licenseWiz2Form.txtSelectedLicenses}'); licTextBox.value = 999; }

 

However, this does not work:

 

function checkLicenses() { var licInputs = document.getElementsByName('license'); var licTextBox = document.getElementById('{!$Component.licenseAssignments.licenseWiz2Form.txtSelectedLicenses}'); licTextBox.value = 'hello'; }

 and it breaks all the other javascript on my page.

 

It's really starting to drive me up the wall! Does anyone have any suggestions as to what could be causing the problem?

 

Thanks,

 

Andy

 

 

 

 

 

 

andyeandye

By the way, I'm doing exactly that in another function and it works just fine:

 

function checkMe(checkbox) { var inputs = document.getElementsByName('product'); var textBox = document.getElementById('{!$Component.licenseAssignments.licenseWiz2Form.txtSelectedProduct}'); var numberOfBoxes = inputs.length; textBox.value=''; ...

 I've tested the code outside of Salesforce (in just a standard html page running locally) and it works absolutely fine. I'm really struggling to see what the problem is.

 

Thanks,

 

Andy

 

 

 

andyeandye

Update! It's now working but still has strangeness...

 

My final function is this:

 

function checkLicenses() { var licInputs = document.getElementsByName('license'); var licTextBox = document.getElementById('{!$Component.licenseAssignments.licenseWiz2Form.txtSelectedLicenses}'); licTextBox.value = null; for (count = 0; count < licInputs.length; count++) { if (licInputs[count].checked) { licTextBox.value += licInputs[count].id + ' '; } } }

 That all works fine so I'm happy. Still, if I put in 

licTextBox.value = 'something';

 then it breaks. Very odd.

 

Anyway, all working now so I can crack on!

 

Andy

 

 

 

JimRaeJimRae

Maybe I am missing something, but it appears to me that your inputtext box is bound to a number field.  This would explain why you could put a numeric value but not text.

Just a thought.

andyeandye

Hi Jim,

 

Thanks for the reply. No, the inputText field is bound to a string. It is now working with a string with the last bit of code. It's just strange that as soon as I put in 'something else' it breaks!

 

Andy

bob_buzzardbob_buzzard
When you introduce the code that fails, what JavaScript error do you get?
bob_buzzardbob_buzzard

I've been playing around with your JavaScript on a test page and haven't got anywhere near reproducing this.

 

The fact that the rest of the JavaScript on the page breaks implies that the browser doesn't think that it is well-formed JavaScript and stops parsing at that point, rather than a problem with assigning the data to the elements value.

 

Can you share any more of the page with us?  I'd like to understand why this is happening in case it bites me!

 

 

andyeandye

You're right - it did look like the browser didn't think it was well-formed because all the javascript on the page stopped working. My entire <script> block looks like this:

 

<script language="javascript"> window.onload = function() { var productsTextBox = document.getElementById('{!$Component.licenseAssignments.licenseWiz2Form.txtSelectedProduct}'); var licTextBox = document.getElementById('{!$Component.licenseAssignments.licenseWiz2Form.txtSelectedLicenses}'); productsTextBox.value = ''; licTextBox.value= ''; } function checkMe(checkbox) { var inputs = document.getElementsByName('product'); var textBox = document.getElementById('{!$Component.licenseAssignments.licenseWiz2Form.txtSelectedProduct}'); var numberOfBoxes = inputs.length; textBox.value=''; if (checkbox.checked) { for (var i=0; i<numberOfBoxes; i++) { if (inputs[i].id != checkbox.id) { inputs[i].checked = false; textBox.value = checkbox.id; } } } } function checkLicenses() { var licInputs = document.getElementsByName('license'); var licTextBox = document.getElementById('{!$Component.licenseAssignments.licenseWiz2Form.txtSelectedLicenses}'); licTextBox.value = null; for (count = 0; count < licInputs.length; count++) { if (licInputs[count].checked) { licTextBox.value += licInputs[count].id + ' '; } } } </script>

  • The window.onload function just resets my text boxes in case they have anything in them.
  • checkMe basically just ensures that I can only have one checkbox selected from a group on the page and puts the id of that checkbox into a hidden field on the page (textBox) (I probably should have called that something more descriptive!).
  • checkLicenses uses a different group of checkboxes on the page. This one allows me to have as many selected as I like in the group and puts the id of each checked box into a hidden field, separated by spaces.

 

It is in checkLicenses where the problem occurred. Where I reset the value of the textbox (licTextBox.value = null; ), if I put in there licTextBox.value = '' all the javascript failed.

 

I've just read it back as I pasted it into this message and I still can't see why it failed. Thankfully it's working at the moment and I can get on with what I need to finish!

 

I'm happy to paste the rest of the page in here if it would be helpful but there's no more javascript, just a couple of onClick events on checkboxes that call the functions.

 

Andy

bob_buzzardbob_buzzard

Don't worry about posting the rest of the page - as you say, all the javascript looks fine.  It smacks of some strange race condition to me.  I sometimes see error messages about unclosed divs after changing visualforce pages. 

 

At least I know how to get around it if it happens.

 

 

prathap raoprathap rao

I am having another issue

 

Please help me out

 

I am using the following javascript

 

<script type="text/javascript" language="JavaScript">
            function Copytoreferredby(val)
            {
            if(val.checked)
            {
               var prescaddress    = document.getElementById('{!$Component.pgblk.ReferredBy.Referred_By_Person_gne__c}').value;
               var Referraladdress = document.getElementById('{!$Component.pgblk.ReferredBy.Case_Referral_City_gne__c}').value;
                if(prescaddress != null)
                {
                    
                    //alert(Referraladdress + prescaddress)
                    Referraladdress = "bnjkbnj,njkkjnkjbnjk";

The Input field here is null still               
                }
            }
            }
            </script>

 

I am getting the alert function working fine, but I am calling this method from a input text box where i am populating one text box from another :(

 

<apex:inputfield id="Referred_by_data_populate" onclick="Copytoreferredby(this)" value="{!Case.Same_as_Prescriber__c}"/>

Is there something I am missing here?

prathap raoprathap rao

A small Correction here

 

<script type="text/javascript" language="JavaScript">
            function Copytoreferredby(val)
            {
            if(val.checked)
            {
               var prescaddress    = document.getElementById('{!$Component.pgblk.ReferredBy.Referred_By_Person_gne__c}').value;
               var Referraladdress = document.getElementById('{!$Component.pgblk.ReferredBy.Case_Referral_City_gne__c}').value;
                if(prescaddress != null)
                {
                    
                    //alert(Referraladdress + prescaddress)
                    Referraladdress = prescaddress   ;

The Input field here is null still               
                }
            }
            }
            </script>

 

I am getting the alert function working fine, but I am calling this method from a input text box where i am populating one text box from another :(

 

<apex:inputfield id="Referred_by_data_populate" onclick="Copytoreferredby(this)" value="{!Case.Same_as_Prescriber__c}"/>

Is there something I am missing here?
Prathap Rao