I have written a code in javascript which creates a p
attribute dynamically along with a button which will remove the content. The function removeValue
works fine in Chrome and Firefox but does not work in IE. Also, the setAttribute('style','')
is not working in IE either. Lastly when I send the values to another page using window.location
it sends undefined
instead of the text.
Everything seems to work fine in Firefox and Chrome but I can't get it to work in IE (Currently using IE 7). How can I solve this issue?
The code:
function removeValue(ob)
{
ob.parentNode.parentNode.removeChild(ob.parentNode);
}
function throwval(obj)
{
var sent_id = obj.id; //get id of the button
var v = document.getElementById(sent_id).value;
var newp = document.createElement("p"); //create a new <p> tag
var text = document.createTextNode(v);
var buttonnode= document.createElement('input');
buttonnode.setAttribute('type','button');
buttonnode.setAttribute('name','del');
buttonnode.setAttribute('value','Remove');
buttonnode.setAttribute('style','background-color: Transparent;width: 125;color: blue;border:0');
buttonnode.setAttribute('onclick','removeValue(this)');
newp.appendChild(text);
newp.appendChild(buttonnode);
document.getElementById("getselected").appendChild(newp); //append the new <p> tag in the div
}
function sendvalues()
{
var div_val = document.getElementById("getselected");
if(!div_val.getElementsByTagName("p").length)
{
alert("Select a value");
}
else
{
//get seperate values of the paragraph inside div
var str="|";
for (i=0; i < div_val.getElementsByTagName("p").length; i++)
{
var paragraphs = div_val.getElementsByTagName("p");
if(!paragraphs.item(i).textContent)
{
var pvalues = paragraphs.item(i).innerText;
}
else
{
var pvalues = paragraphs.item(i).textContent;
}
//var sendpvalues = "products=" + pvalues;
// alert(pvalues);
str = str + pvalues + "|";
//alert (str);
//ajaxOb.send(sendpvalues);
}
// alert(str);
window.location="send_data.php?str="+str;
}
}
Turns out that IE supports 'innerText
' and firefox supports 'textContent
'. I fixed the undefined
issue by using 'if
' statement. Code updated