joe92 Posted December 19, 2011 Share Posted December 19, 2011 Does anyone know of a way that JavaScript could figure out whether it is to use .innerHTML or .value with a command? I.e. I have one function that is to change the contents of a group of elements within a for loop. I don't know whether the element that is currently being changed is going to be a div or a textarea so I would need to be able to figure out on the fly whether to use the .innerHTML or .value command with my function. Thanks for any help, Joe Quote Link to comment Share on other sites More sharing options...
sunfighter Posted December 19, 2011 Share Posted December 19, 2011 Changing the contents of a group of elements has to be triggered by something and that should tell you what is to be changed. JS can't figure anything out, the programmer does this and controls js maybe with IF statements. Looik at what ever is triggering the change and see if you can figure out what is to be changed. If you can't figure it out then post the code and we'll look at it. Quote Link to comment Share on other sites More sharing options...
joe92 Posted December 19, 2011 Author Share Posted December 19, 2011 Got it sorted. When you locate the element on the page and then ask to display it, it will display something like [object HTML....Element]. By converting this into a string I could use the javascript match to look for which type of element it was and act accordingly. After looking at a list of every single tag available (and testing on tags I didn't know) I found that the only tags that actually need javascripts value to display on screen the contents are input tags and textareas. Here is my code should anyone run into a similar problem: <?php /*locate element*/ var element = document.getElementById(id); /*turn [object HTML....Element] into a string that says just that*/ var el = new String(element); /*match the string with value elements to produce value as the output*/ if(el.match(/Input/g) || el.match(/TextArea/g)) { /*fill the value*/ element.value = thisItem; } /*since the huge majority of elements are actually filled using innerHTML, this is the else clause*/ else{ /*fill innerHTML*/ element.innerHTML = thisItem; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.