Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  90] [ 5]  / answers: 1 / hits: 38140  / 13 Years ago, thu, february 2, 2012, 12:00:00

I Have a text box and a control button next to it.Clicking on the button will call another function where some data is selected and written back to the text box.Currently my text box is editable by the user after the data is fetched to the text box.I want to make the text box read only after the user clicks on the button and fetches the data to the text box,So that now the data is present in the text box which cannot be edited.If the user has to change the data,the user has to again click on the button to call the function which will help us over write the data in the text box.How can I accomplish this in javascript.


More From » html

 Answers
30

you can do it like this



HTML:



<input type=text id=mytextbox value=click the button below./>
<input type=button value=click onclick=changeText();/>


Javascript:



function changeText(){
var text_box = document.getElementById('mytextbox');
if(text_box.hasAttribute('readonly')){
text_box.value = This text box is editable.;
text_box.removeAttribute('readonly');
}else{
text_box.value = This text box is read only.;
text_box.setAttribute('readonly', 'readonly');
}
}


fiddle example: http://jsfiddle.net/b7jAy/


[#87682] Tuesday, January 31, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;