How do I replace text inside a div element?

I need to set the text within a DIV element dynamically. What is the best, browser safe approach? I have prototypejs and scriptaculous available. <div id=”panel”> <div id=”field_name”>TEXT GOES HERE</div> </div> Here’s what the function will look like: function showPanel(fieldName) { var fieldNameElement = document.getElementById(‘field_name’); //Make replacement here } 14 Answers 14

How to get a DOM Element from a jQuery selector?

I’m having an impossibly hard time finding out to get the actual DOMElement from a jQuery selector. Sample Code: <input type=”checkbox” id=”bob” /> var checkbox = $(“#bob”).click(function() { //some code } ) and in another piece of code I’m trying to determine the checked value of the checkbox. if ( checkbox.eq(0).SomeMethodToGetARealDomElement().checked ) //do something. And … Read more

Difference between document.addEventListener and window.addEventListener?

While using PhoneGap, it has some default JavaScript code that uses document.addEventListener, but I have my own code which uses window.addEventListener: function onBodyLoad(){ document.addEventListener(“deviceready”, onDeviceReady, false); document.addEventListener(“touchmove”, preventBehavior, false); window.addEventListener(‘shake’, shakeEventDidOccur, false); } What is the difference and which is better to use? 4 Answers 4

Using querySelectorAll to retrieve direct children

I am able to do this: <div id=”myDiv”> <div class=”foo”></div> </div> myDiv = getElementById(“myDiv”); myDiv.querySelectorAll(“#myDiv > .foo”); That is, I can successfully retrieve all the direct children of the myDiv element that have class .foo. The problem is, it bothers me that I must include the #myDiv in the selector, because I am running the … Read more

How to get the element clicked (for the whole document)?

I would like to get the current element (whatever element that is) in an HTML document that I clicked. I am using: $(document).click(function () { alert($(this).text()); }); But very strangely, I get the text of the whole(!) document, not the clicked element. How to get only the element I clicked on? Example <body> <div class=”myclass”>test</div> … Read more