Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

readonly field value change

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi


I have form with few of the fields set as read only.
I need to read the value of the read only feild from javascript.
Could anyone help me with that.

Thanks
Sravani
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No differently from any other non-readonly element.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading the value is just like any other form



If you want to have an onchange handler on it as your title suggests, onchange does not fire when JavaScript changes form field values. You need to trigger it in the code that sets the hidden form element's value.

Eric
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried using var test = document.getElementById("readonly-id");
the value of test is undefined.


 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you don't have an element with the id value of readonly-id, or you are trying to retrieve it before it is created.
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Pascarello wrote:Reading the value is just like any other form



If you want to have an onchange handler on it as your title suggests, onchange does not fire when JavaScript changes form field values. You need to trigger it in the code that sets the hidden form element's value.

Eric




Hi Eric

I tried all the above mentioned options but still the value is undefined.

Could you help me with this.

Thanks
Sravani
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Then you don't have an element with the id value of readonly-id, or you are trying to retrieve it before it is created.



I have the element with this id in the form and the value is MS.
Since it is read only field the value is being populated on the page.
But I need to read that value.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sravani gogineni wrote:I have the element with this id in the form and the value is MS.

So you say. Prove it.

If such an element really does exist, are you trying to get its value before the element exists?
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

sravani gogineni wrote:I have the element with this id in the form and the value is MS.

So you say. Prove it.

If such an element really does exist, are you trying to get its value before the element exists?




Chart Code field is read only here and but i am not able to read the value of it as 'MS'
it get it as undefined.
Filename: test.bmp
File size: 983 Kbytes
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's just a picture. To work on the problem you actually have to look at the HTML which you are saying is a problem. And so, obviously, do we.
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:That's just a picture. To work on the problem you actually have to look at the HTML which you are saying is a problem. And so, obviously, do we.



I am trying to read the value from a form as document.getElementByid('id') in my js.
I could see the <span id="" /> present in the form using firebug but the value is still null.
Could someone please tell me why i am unable to get the value of it.

Thanks
Sravani
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will not download images. Post the code (UseCodeTags) or the chances of you getting help are pretty slim.
 
Paul Clapham
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sravani gogineni wrote:I am trying to read the value from a form as document.getElementByid('id') in my js.


That wasn't what you said before.

I could see the <span id="" /> present in the form using firebug but the value is still null.


Look carefully and you'll see that the value of the "id" attribute is an empty string.

Could someone please tell me why i am unable to get the value of it.


Somebody who could see the HTML in question probably could. Right now you're the only one in that category. If you want this question answered I suggest you get serious and stop posting useless information.
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I will not download images. Post the code (UseCodeTags) or the chances of you getting help are pretty slim.



I am sorry. Here is the code.

<span id="test-id">MS </span>

function test(){

var test1 = document.getElementById('test-id');
alert('test'+test1.value);
}

i get the an alert with test undefined. Please help.

Thanks!
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From where is test() called?
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Paul Clapham
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sravani gogineni wrote:I have form with few of the fields set as read only.
I need to read the value of the read only feild from javascript.
Could anyone help me with that.



This is what your original post said. You posted some HTML which doesn't have any such form. So which is right?
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

sravani gogineni wrote:I have form with few of the fields set as read only.
I need to read the value of the read only feild from javascript.
Could anyone help me with that.



This is what your original post said. You posted some HTML which doesn't have any such form. So which is right?




'MS' is the value i am talking about as read only field. i need to read that 'MS' using javascript.
Hope i am clear now.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not a "read-only field", it's a span. Please use proper terminology or you just waste everyone's time including your own.

Still waiting for an answer to my question.
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:That is not a "read-only field", it's a span. Please use proper terminology or you just waste everyone's time including your own.

Still waiting for an answer to my question.



ok i mean span. how i read the value of the span in javascript.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the feeling that there is a large amount of frustration from both sides in this discussion.

From our side, we need a SSCCE - without this, we are wasting our time. Sravani - probably the best use of your time is to follow the link for SSCCE and the link for HowToAskQuestionsOnJavaRanch and understand what it is that we need in order to help you. Bear asked where test() is called from - your code snippet does not show this, so we don't know if the JavaScript is being called before the page has rendered, after it has rendered, or undefined. This could make a huge difference in how we answer your questions.

To give you an example of an SSCCE, here is some sample code that can be put into a standalone HTML file and loaded into a browser (note that the code snippet you posted cannot be pasted into a standalone HTML file - nothing will happen if you load it in the browser, since the test() function is not called in your code (hence Bear's question)):

If I were to post a question based around this snippet, the other members of this forum would have a chance of answering it, or of telling me where I am going wrong in my coding practices.
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew Monkhouse wrote:I get the feeling that there is a large amount of frustration from both sides in this discussion.

From our side, we need a SSCCE - without this, we are wasting our time. Sravani - probably the best use of your time is to follow the link for SSCCE and the link for HowToAskQuestionsOnJavaRanch and understand what it is that we need in order to help you. Bear asked where test() is called from - your code snippet does not show this, so we don't know if the JavaScript is being called before the page has rendered, after it has rendered, or undefined. This could make a huge difference in how we answer your questions.

To give you an example of an SSCCE, here is some sample code that can be put into a standalone HTML file and loaded into a browser (note that the code snippet you posted cannot be pasted into a standalone HTML file - nothing will happen if you load it in the browser, since the test() function is not called in your code (hence Bear's question)):

If I were to post a question based around this snippet, the other members of this forum would have a chance of answering it, or of telling me where I am going wrong in my coding practices.




Thanks Andrew!
Here is the code snippet i am using:


the alert i see is : testundefined
Please help!
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew beat me to it, I was about to post that at this point we need an SSCCE.

Without one, we are just spinning our wheels. Please post a complete HTML page that demonstrates the issue. Be sure to read the link for SSCCE.
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sravani gogineni wrote:



span and div tags don't have a value attribute ... try using innerHTML instead of value

 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. If it was a read-only field as originally reported, as opposed to a span, that would have been clear from the start.
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lalit Mehra wrote:

sravani gogineni wrote:



span and div tags don't have a value attribute ... try using innerHTML instead of value



Thanks Lalit!
That helped me. One more question I have.
innerHTML returns span value with some spaces and   in it. how do i remove those?

Thanks in advance!
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really need us to tell you how to remove spaces from a string?

And a lesson to be learned from this topic: attention to detail and correct terminology would have made this much much shorter.
 
sravani gogineni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Do you really need us to tell you how to remove spaces from a string?

And a lesson to be learned from this topic: attention to detail and correct terminology would have made this much much shorter.



trim() didnt help ...i want to remove
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use JavaScript regular expression with replace().

Eric

 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you just got the answer above ... say thanks to Eric
 
No, tomorrow we rule the world! With this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic