views:

58

answers:

2

Hi everyone I have a strange Behaviour issue with Sharepoint.

I'm testing some javascript in the content editor web part and ran accross this issue. When Creating DOM Elements dynamically in a normal HTML Page as shown below, I am able to retrieve the values from the created Element.

When I however try this in a Sharepoint Content Editor web part, I get the JS error back from the SharePoint Page stating : "0.value1" is null or not an object.

Any idea why this is happening?

<head>
<script type="text/javascript">
function WriteElements() 
{
    var input = document.createElement("input");
    input.setAttribute("type", "hidden");
    input.setAttribute("name", "BLABLA");
    input.setAttribute("value1", "ASDFASDFZXCV");

    document.getElementById('theUL').appendChild(input);
    var i = document.getElementsByName("BLABLA");
    alert(i[0].value1);
    return;
}
</script>
</head>
<body>
<ul id="theUL"></ul>
<p><input id="Button1" type="button" value="button" onclick="WriteElements();"/></p>
</body>

A: 

I can't tell you exactly what the problem is. My guess is that IE isn't reparsing the DOM soon enough, so document.getElementsByName fails.

This works for me:

<script type="text/javascript">
function WriteElements() {
    var input = document.createElement("input");    
    input.id = "BLABLA";    
    input.setAttribute("type", "hidden");
    input.setAttribute("value1", "ASDFASDFZXCV");

    document.getElementById('theUL').appendChild(input);
    var button = document.getElementById("BLABLA");
    alert(button.value1);
    return;
}
</script>
<ul id="theUL"></ul>
<p><input id="Button1" type="button" value="button" onclick="WriteElements();"/></p>
Chloraphil
A: 

In fact, even if you create a "normal" html page with the code you posted above; It doesn't work.

value1 is not a property of the object of type "input", it is an attribute and it's retrieved in a different fashion.

Luj Reyes