Jump to content

[HTML DOM] object question


Jack McKalling
 Share

Recommended Posts

The Textarea object has the readOnly property. If I want to give it the fixed value, I do it like this:

<textarea readonly="readonly" ...>
But when I want it to do by JavaScript, I do it like this (with conditional operator):
var T = document.getElementsByTagName("textarea")[0]T.readOnly = (T.readOnly == "readonly") ?"" :"readonly"
But that does not work :) I want it to be switched readonly when this script executes, so when already readonly it has to change into not-readonly :)How? :(
Link to comment
Share on other sites

well... you seem to use some stuff I'm not familiar with, but I think I might be able to help...I'm not sure about javascript but I know with other languages I've used, you cant do:var T = document.getElementsByTagName("textarea")[0]you have to just do this:var T = document.getElementsByTagName("textarea")T[0].readOnly = (T.readOnly == "readonly") ?"" :"readonly"and then... I've never seen- (T.readOnly == "readonly") ?"" :"readonly" -before... but you might just tryif(T[0].readOnly == "readonly") T[0].readOnly="";hope that helps

Link to comment
Share on other sites

Nope :D I was right, you wrong :)It appears I am a little more advanced then you, no offence, but it IS like what I wrote.You CAN do var SomeThing = document.getElementsBySomeWay("something")[0]This way you store that object in the variable, and not the array in which it is contained :(Also, the mass unreadable characters you couldn't understand, LOL :)That was called the Conditional Operator.Something = (condition) ?"assign value when true" :"assign value when false"Explanation: if I do it like what I wrote, the readonly property will be switched both from readonly to readable AND back by the same scriptline :)

Link to comment
Share on other sites

I just usually do this:In the <script> I assign a function like so:function setReadOnly() { document.getElementById("textarea").readonly="readonly"; }And then I'll assign to the <body> an onLoad event:<body onLoad="setReadOnly();">And then with <textarea>, I do this:<textarea id="textarea" cols="blah" rows="blah"><!--sometext--></textarea>On a personal note, I don't usually use textareas. But with most tags, I usually do this sort of thing. (Especially with .style for DOM and stuff like that.) BTW - If that don't work, it should. SPELLING COUNTS!

Link to comment
Share on other sites

Nope :D I was right, you wrong :)It appears I am a little more advanced then you, no offence, but it IS like what I wrote.You CAN do var SomeThing = document.getElementsBySomeWay("something")[0]This way you store that object in the variable, and not the array in which it is contained :(Also, the mass unreadable characters you couldn't understand, LOL :)That was called the Conditional Operator.Something = (condition) ?"assign value when true" :"assign value when false"Explanation: if I do it like what I wrote, the readonly property will be switched both from readonly to readable AND back by the same scriptline :)

Well cool, I learned some new syntax! I'll have to remember that conditional operator thing.
Link to comment
Share on other sites

True :) But I noticed myzelf :):blink: Just ask its value with javascript from the url :(When the page has loaded, rewrite the url to this:

java script:alert(document.getElementsByTagName("textarea")[0].readOnly)
I got an alert with this, saying "true", while in Xhtml, the element had an attribute readonly added. When I removed that readonly="readonly" attribute, the alert said "false"So indeed true and false :D The fact that the attribute had its name as a value confused me with the value it has as an property at the DOM structure.You were too late, I found it out myself. Still, thank you for the correct sollution :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...