function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael RotterMichael Rotter 

Update a text input field using javascript

I'm trying to update the data in an input text field using a button and javascript. I've tried using document.getElementByID but i get an error saying that isn't a function. Here is my code so far:

document.getElementByID("Case.Subject").value="Does this work?";

function CopySubject() {
var a = "{!Case.Subject}";
alert(a); 
}

function CopyCN() {
var b = "{!Case.CaseNumber}";
alert(b); 
}

CopySubject();
CopyCN();

Essentially, my code should put "Does this work?" into the subject of the case and then put out messages that contain the subject and case number. Any help is greatly appreciated!
atul patil 7atul patil 7
Hello Michael ,
document.getElementByID("Case.Subject").value="Does this work?"; this is not going to work

refere this code:-
<apex:inputText id="CaseSubject"  value="{!Case.Subject}">​</apex:inputText>

<script>
function CopySubject() {
var CaseSub= $('[id$=CaseSubject]').val();
alert(CaseSub); 
}
​</script>

You have to call CopySubject(); this function on click of any button 

Thanks,
Atul Patil
Salesforce Developer
www.zen4orce.com
Michael RotterMichael Rotter
Hey Atul,

Thanks for the reply and the suggestions. Is there a way to do it completely in javascript tho? The only scriptable action option on the edit button page doesn't recognize the html syntax.

Thanks!
Amol Salve 14Amol Salve 14
Hello Michael,
        
         Use input field and then call function from javascript on button click, this is will update field value onClick


Enter New Value: <input type="text" id="myText" value="{!Case.Subject}">

<button onclick="myFunction()">Update Value</button>

<script>
function myFunction() {
    document.getElementById("myText").value = "Does this work?";
}
</script>

Thank you,
Amol Salve
Salesforce Developer
atul patil 7atul patil 7
Hello Michael Rotter,
      You can also do this by using JQuery
     Please refere this code :
 
Enter New Value: <input type="text" id="myText" value="{!Case.Subject}">

<button onclick="myFunction()">Update Value</button>

<script>
function myFunction() {
   var CaseSub= $("myText").val();
  alert(CaseSub); 
}
</script>
Thanks,
Atul Patil
Salesforce Developer
www.zen4orce.com
 
Mustafa JhabuawalaMustafa Jhabuawala
Michael,

I think there is some confusion goin over here.

Are you trying to achieve this in your VF page or using JavaScript button on the standard page ?

If you are using VF Page then - 
  1. You need to give ID to your input field
  2.  And then you can update the input field using JavaScript

If you are using JavaScript button then, in that case this is not achievable because you can't set ID on the input field of standard page.

Why I guessed that you might be using Javascript button, is because the code you have shared gives me a hint of JS button -
document.getElementByID("Case.Subject").value

Thanks,
Mustafa Jhabuawala
Technica Lead at Zen4orce (http://www.zen4orce.com)
Thomas PiteraThomas Pitera
I have 2 things im trying to do, with pure javascript change the value of a textbox and a dropdown menu
i tried your suggestion, i get this error
User-added image
is there another way to do it?
I want my sf form to be green (showing that the edits have been made successfully)
User-added image
I was thinking maybe something involving sfdcPage.inlineEditData("*id of text field*")
Please help, and please verify the process works for textbox and dropdown. thank you