Many modern libraries implement this type of function (I know prototype does), but it's a great one to have in your common.js in case you don't need a full library for projects.
The basic idea is that using .innerHTML to insert new elements is very convenient because you can simply provide a string of the new HTML, but you lose the precision of DOM insertion (for instance, if you want to insert a new element in the middle of a bunch of DOM siblings, you have to replace them all in the parents innerHTML).
Conversely, using DOM methods like insertBefore() are very precise but require you to make the entire structure with createElement calls. This can be a LOT of code when all you want to do is insert a new anchor element.
This elegant little solution combines both methods by riding on the fact than when you replace an element's innerHTML, the browser creates the appropriate DOM hierarchy for the new elements on the fly. So this function takes a string of HTML you want to make into a DOM structure, changes the innerHTML of a dummy element to render this structure, and then returns the first child.
// fromHTML()
function fromHTML(html) {
var root = document.createElement(‘div’);
root.innerHTML = html;
return root.firstChild;
}
Very simple to use, and very handy:
var node = fromHTML(’<a href=”home.html” title=”Going Home”>
<strong>There’s no place like it</strong></a>’);
// Now you can use the precision of DOM methods without having
// to create the entire structure by hand:
element.insertBefore(thing, node);
I have a onClick event in Javascript, What I would like to do is if a PHP variable were to be set, to trigger the Javascript onClick event. Is there a way to simulate an onClick? [URL]I have a form with some non-showing (non hidden) input fields but they show if I click a + and hide if I click -, default is hide, but if a certain PHP var were set I want to somehow simulate that + was clicked. I tried to force 'var lText' but that got the whole thing stuck in one state.
I have two jQuery scripts running on the same page. One of them dynamically assigns height parameters to the wordpress page. The other is a slideshow script which makes images transition from one to the next in a slideshow.When the slideshow is inserted into the wordpress page, it works fine, but in IE7 there is extra page height whenever there is a slideshow on the page. In other browsers it is fine, and on other pages that dont have a slideshow the page height is fine.Looking at the source code, it seems that the dynamically assigned div height paramaters that are injected into the slideshow divs by the jQuery slideshow script are getting involved in other divs on the page, influencing their height.It seems that the slideshow script is interfering with the page height script. how can I make it so they dont entangle? Here is the page height jQuery script that assigned dynamic height to the "art-content-layout-row" div (the div that controls page height)
Code:
/* begin Layout */ jQuery(function () { if (!jQuery.browser.msie || parseInt(jQuery.browser.version) > 7) return;
[code]...
When looking at the html source in the page, the slideshow script inserts this jQuery unique number beside the dynamically added div parameters in the slideshow divs like this:
It turns out that this same unique number is assigning itself to the art-content-layout-row div which is supposed to be controlled by the other script.I am wondering if the fact that these unique id numbers being the same is causing the issue, as though the system is thinking that these two scripts are somehow the same one?Both scripts are in different directories and called separately in the page footer.
Many time i am getting one problem with Jquery when i am using more then one jquery plugin on same page then only one plugin works which will be on top. I guess few variables mixing and disturbing to each other. I just want to know is there any way to control it?
Is it possible to have secured (SSL/HTTPS) and non-secured (HTTP) content in the same page without breaking the security? I am developing a secured reservation system in which the user can access Google Maps (not secured). The map is embedded in the page and does not open in a separate window.
What solution do you recommend? Use iframes? If so, how?
Is there a way to add an if and else with in innerHTML like this. var sfe = document.getElementById("mainform").innerHTML ='<form name="'+frmName+'">'+ '<!-- comment -->'+ '<h2 id="pa" name="dr">';if(n == 1){'+fname+'}else if(n == 2){<b>No Title</b>} '</h2>'+ 'more'+
I am getting data from two tables in a database using a dropdown box with a onchange and some ajax to update two diferent Div tags, basically the id is passed to a php page,, I grab the data from the two tables, I then echo the data in two html tables. I echo a ** for a delimiter between the two tables,
Then I use a javascript split function to split the responseText into two peices so I can update each DIV with the coresponding data.
Everything works in Firefox, but in IE7 only one div gets updated with its data, the other div will not change, and I get a unknown runtime error. However if I go into my php page and change the data I am echoing after the delimiter to a simple echo 'test' it will work. Code:
I've got this code that creates a new new row and cell. I then put some text into the cell with innerHTML - works beautifully with Firefox but fails with IE. I guess IE doesn't support this way of doing it, but is there another way of doing it with DOM?
Here is my problem in a nutshell: a script to model dynamic table extension. It works under Firefox. But IE just aborts, complaining about an "unknown runtime error" in the line with "innerHTML". Why?
<html><head></head><body>
<script language="javascript"> function extend() { var tb = document.getElementById('thetable').tBodies[0]; var newrow = document.createElement('tr'); tb.insertBefore(newrow,tb.rows[tb.rows.length-1]); tb.rows[tb.rows.length-2].innerHTML = '<td>A</td><td>dummy</td><td>row</td>' } </script>
Why doesn't a SELECT element's innerHTML reflected which option was selected? Works in IE. I need this functionality so that I can retain what choices a user made in a tabbed interface.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://w3.org/1999/xhtml"> <head>
<script language="javascript"> function callAlert(){ var theHTML = document.getElementById('Radius').innerHTML; //alert(theHTML); } </script> <title>Untitled Document</title> </head>
I want to load an external file into my page via XMLhttpRequest and innerHTML. The external file contains javascript code which is not executed during or after insertion. All the HTML markup is displayed just fine.... basicly even this code doesnt work:
var img = document.createElement("img"); img.attachEvent("onclick",alert("test")); var div = document.createElement("div"); div.appendChild(img); //can't work; div.innerHTML="<-click this";
but i use attachEvent like this,it work; eg:
var img = document.createElement("img"); img.attachEvent("onclick",alert("test")); var text = document.createElement("span"); text.innerHTML="<-click this"; var div = documet.createElement("div");
div.appendChild(img); //can work div.appendChild(text);
How can I get the innerHTML of a <div> area only when the page loads, then use that variable in a function?
Here is my code: function setContent(zz) { var lb = document.getElementById('leftbar').innerHTML; var rb = document.getElementById('rightbar').innerHTML; document.getElementById("myContent").innerHTML = "<span class="title_Page">"+Page[zz]+"</span>"; if (zz=="home") { document.getElementById('leftbar').innerHTML = lb; document.getElementById('rightbar').innerHTML = rb; document.getElementById('leftbar').style.width = 食px' document.getElementById('rightbar').style.width = 食px' } else { document.getElementById('leftbar').innerHTML = ""; document.getElementById('rightbar').innerHTML = ""; document.getElementById('leftbar').style.width = Ɔpx' document.getElementById('rightbar').style.width = Ɔpx' } }
I want lb and rb to be set only once (i.e. only when the page loads the first time). I tried putting those variables outside the function, but when I do they, they show up as undefined. So how could i code it so that when the page loads, it assigns the innerHTML to the two variables, but not any other time while that page is open?
Can somebody explain why this happens, and how I can solve it? Since the first case works on all browsers I tried, I was hoping to get a browser-independant method to read the sourcecode of another file. By the way, all files are local.
I'm using an i-frame to grab a server-side text file and display its content elsewhere in the html document. On change of the i-frame source, I want to access its innerHTML.
The i-frame source changes just fine and even displays the source; the glitch is in accessing the innerHTML for that new i-frame src file. It requires *TWO* clicks of the onClick element to get the correct innerHTML (with just one click, the source will change, and display the new file in the html document; however, the innerHTML is still that of the old source file for the i-frame).
This does not appear to be a loading problem with the new src file into the i-frame. (ie: setTimeout on a function to get the innerHTML after the src file loads does not work).
So the question is: how can I get the i-frame source to change AND access the new src file's innerHTML with one click?? Code:
so I have this code for a date form and I made a PHP loop and even a javascript loop for it. There is a script that supposedly adds multiple 'dates' when the user clicks this 'add more date' button and I use an innerHTML to clone it.
My problem is how will I put it inside an innerHTML on a javascript. Since innerHTML reads html tags, is it possible to put a PHP loop within it? and how will you also call a javascript function within it?
this is the PHP code: <select> <?php for ($i=date("Y"); $i>=1950; $i--)
[Code]....
I just need to know if what im doing is right, or is there another way to do this?
I'm working on a script that inserts things into tables. It's complicated and involves xmlhttp and lots of stuff, but I've narrowed the problem down to something simple. The following code does not work in IE. It doesn't generate an error, but simply displays nothing. Here is the code:
I'm trying to hide a TR if a TD within it has only specific html in it. Why would this not work: if(document.getElementById('website1').innerHTML.toLowerCase()=="<a href='' title=''></a>") { document.getElementById('website-row').style.display='none'; }