aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes readonly field value change Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "readonly field value change" Watch "readonly field value change" New topic
Author

readonly field value change

sravani gogineni
Ranch Hand

Joined: Feb 09, 2011
Posts: 33
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

No differently from any other non-readonly element.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Feb 09, 2011
Posts: 33
I tried using var test = document.getElementById("readonly-id");
the value of test is undefined.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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

Joined: Feb 09, 2011
Posts: 33
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

Joined: Feb 09, 2011
Posts: 33
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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

Joined: Feb 09, 2011
Posts: 33
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.


[Download test.bmp] Download

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Feb 09, 2011
Posts: 33
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

I will not download images. Post the code (UseCodeTags) or the chances of you getting help are pretty slim.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Feb 09, 2011
Posts: 33
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

From where is test() called?
sravani gogineni
Ranch Hand

Joined: Feb 09, 2011
Posts: 33

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Feb 09, 2011
Posts: 33
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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

Joined: Feb 09, 2011
Posts: 33
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.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

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.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
sravani gogineni
Ranch Hand

Joined: Feb 09, 2011
Posts: 33
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

sravani gogineni wrote:


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



http://plainoldjavaobject.blogspot.in
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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

Joined: Feb 09, 2011
Posts: 33
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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

Joined: Feb 09, 2011
Posts: 33
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
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Use JavaScript regular expression with replace().

Eric

Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

you just got the answer above ... say thanks to Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: readonly field value change