Saved Bookmarks
| 1. |
Solve : How do I replace an onscreen element with a value?? |
|
Answer» Given HTML like this: Alabama... Wyoming I want to be able to replace the on-screen "with the "value" upon a selection click. How do I do that? Ideally you would want to use jQuery for this to make life a lot easier but it could be done in raw Javascript if absolutely required. Essentially you would need to bind an event handler to the change event of the select. When the event handler is triggered, simply get the value of the select and then use something like the jQuery replaceWith method to replace the input. http://api.jquery.com/replacewith/Gosh, that seems an incredible amount of work just to do what should be very light weight! Something like TURNING off the .visible property or setting one element equal to another (if the element has an ID = "Fred" setting Fred.value to something like "SylvesterStallone" should replace the element, no? Perhaps something in the DOM object? Just curious as to what you what to do. Do you already have a lo-tech way of doing it? Example: Three PAGES. All three pages have common things. They look alike. First page gets something and the will then goo either page 2 or page3 as needed. Does that describe what your want? Unfortunately that's really as simple as it can be, you have to do DOM manipulation in Javascript, you can't just set variables to make it work.Actually, I think I found the solution but I can't seem to get the syntax. Presume I have a with the long form states In a list as pairs with the two letters abbreviations in each state' s value. Using "WithReplace", and the select being called "regstater" what would be the correct syntax to have in the select's onclick= event? I know I'm on the right track but my mind is going tapioca pudding! How much JavaScript do you know? I'd suggest getting reasonably comfortable with it before attempting this. You shouldn't really be putting much code in the onclick attribute (ideally not using it at all and using JS event handlers). The main bulk of your JavaScript logic should be in a separate JavaScript file loaded into the page. > How much JavaScript do you know? Well, I prefer *real* programming languages instead... Seriously, what is the problem with its OnClick event handling? I thought it was pretty robust. No? .I'm not entirely clear on what you are trying to do, myself. It seems clear that you want to completely replace the combobox with the selection in the combobox, but that seems a bit weird, which is why I ask. if it is, you'll want the onchange event, not onclick. And you'll want to change the HTML itself. You can either do this with the DOM replaceChild() method, or you can create an element to encompass the select tag, and replace the innerHTML of that item: Code: [Select]<html> <body> <input type="text" id="xyz" /> <span id="stateselection"> <select size="0" id="regstater" onchange= "stateselection.innerHTML=regstater.value"> <option value="AL">Alabama</option> <option value="WY">Wyoming</option> </select> </span> </body> </html> By the way- this isn't in a form, but the name attribute is only used for form submissions. the id tag is used when dealing with the DOM; Your original script was failing because there was no regstater2 id, for example; but as you have them defined, EVEN if the name was lookup-able, they would be the same item anyway. Not sure what you expected when setting a property to itself Another reason I'm unsure if this is actually the sort of thing you were after is because if it is the textbox sorta just- SITS there and does nothing. If you want to set the value of the textbox to the state when the state combobox is changed, that's fairly similar: Code: [Select]<html> <body> <input type="text" id="xyz" /> <select size="0" id="regstater" onchange= "xyz.value=regstater.value"> <option value="AL">Alabama</option> <option value="WY">Wyoming</option> </select> </body> </html> Quote Seriously, what is the problem with its OnClick event handling? I thought it was pretty robust. No?Nothing really. But it's typically a pain in the *censored* trying to get any non-trivial logic into a STRING attribute. Typically you would write the code in a separate file as a function, include it in the file by using a |
|