Welcome Guest, Not a member yet? Register   Sign In
Javascript not working
#1

[eluser]markanderson993[/eluser]
Hello there CodeIgniter experts, I am trying to get my simple piece of javascript/html code that works on a normal page like <html><head></head><body>...code here</body></html> but does not work when I copy it into my view.

This is the code I am working with

Code:
[removed]
window.onload = initAll;

function initAll() {
    document.getElementsByTagName("form")[0].onsubmit = addNode;
    alert("hi");
}

function addNode() {
    alert("form submitted");
    var inText = document.getElementById("textArea").value;
    var newText = document.createTextNode(inText);

    var newGraf = document.createElement("p");
    newGraf.appendChild(newText);

    var docBody = document.getElementsByTagName("body")[0];
    docBody.appendChild(newGraf);
    
    return false;
}

                        
[removed]

<div style="margin:5px;border-bottom:1px solid #000;padding:5px;font-size:14px;">Ajax Interface</div>

&lt;form action="#"&gt;
<table style="width:100%" cellspacing="5px">
    <tr>
        <td>
                &lt;textarea id="textArea" rows="5" cols="30"&gt;&lt;/textarea>
                <div style="clear:both;height:5px;"></div>
                &lt;input type="submit" value="submit" /&gt;
        </td>
        <td style="vertical-align:top">Add some text to the page</td>
    </tr>    
</table>
&lt;/form&gt;

Does anyone know why it works on a normal page and not in CodeIgniter?

Any help would be greatly appreciated!
#2

[eluser]markanderson993[/eluser]
It seems that this

Code:
document.getElementsByTagName("form")[0].onsubmit = addNode;

is causing the problem. When I add an onsubmit attribute to the form tag everything works perfectly... strange.

Does anyone know why this is happening?
#3

[eluser]slowgary[/eluser]
What do you mean by "it does not work"? Does it not get echo'd onto the page? Does the JS throw an error? Do you have this live somewhere so we can check it out?
#4

[eluser]markanderson993[/eluser]
It does not show any error it simply does nothing when the submit button is clicked. I suppose there was a problem with the code posted directly above with that one line. Since I got it to work.. crisis averted :/




Theme © iAndrew 2016 - Forum software by © MyBB