|
I'm basically using AJAX and returning a bunch of information through XML and creating a checkbox. If the the XML returns that the checkbox should be set to true, check it, otherwise leave it empty. Below is my iteration when i go through the xml document. also i'd like to have the checkbox call a function when clicked, but I can't get it to work in both IE & Mozilla. Thanks for the help. - var chkbox = document.createElement('input');
-
chkbox.type='checkbox';
-
chkbox.setAttribute('name','chkPortfolio');
-
chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
-
chkbox.setAttribute('value',portfolios.childNodes[j].firstChild.data);
-
if( portfolios.childNodes[j].attributes[1].text=="True") {
-
chkBox.onclick= function(evt) { refCell.set
-
}
-
else {
-
chkbox.checked=false;
-
}
-
chkbox.id = "chkPortfolio_" + (j+1);
-
chkbox.li_id = "trPortfolio_" + (j+1);
-
var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
-
listItem.appendChild(chkbox);
-
listItem.appendChild(name);
-
list.appendChild(listItem);
-
}
| |
Share:
Expert 100+ |
I'm basically using AJAX and returning a bunch of information through XML and creating a checkbox. If the the XML returns that the checkbox should be set to true, check it, otherwise leave it empty. Below is my iteration when i go through the xml document. also i'd like to have the checkbox call a function when clicked, but I can't get it to work in both IE & Mozilla. Thanks for the help. - var chkbox = document.createElement('input');
-
chkbox.type='checkbox';
-
chkbox.setAttribute('name','chkPortfolio');
-
chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
-
chkbox.setAttribute('value',portfolios.childNodes[j].firstChild.data);
-
if( portfolios.childNodes[j].attributes[1].text=="True") {
-
chkBox.onclick= function(evt) { refCell.set
-
}
-
else {
-
chkbox.checked=false;
-
}
-
chkbox.id = "chkPortfolio_" + (j+1);
-
chkbox.li_id = "trPortfolio_" + (j+1);
-
var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
-
listItem.appendChild(chkbox);
-
listItem.appendChild(name);
-
list.appendChild(listItem);
-
}
You had quite a few syntax problems but I ironed them out. This will work cross-browser: -
<html>
-
<head>
-
<script>
-
function blah(){
-
var j=0;
-
var chkbox = document.createElement('input');
-
chkbox.type='checkbox';
-
chkbox.setAttribute('name','chkPortfolio');
-
chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
-
chkbox.setAttribute('value',"blah");
-
if( "True"=="True") {
-
chkbox.onclick= function() {alert('hi')};
-
}
-
else {
-
chkbox.checked=false;
-
}
-
chkbox.id = "chkPortfolio_" + (j+1);
-
chkbox.li_id = "trPortfolio_" + (j+1);
-
//var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
-
document.getElementById('body').appendChild(chkbox);
-
//listItem.appendChild(name);
-
//list.appendChild(listItem);
-
}
-
</script>
-
</head>
-
<body id="body" onload="blah()">
-
fdsfsdafasdfdsfsdasda
-
</body>
-
</html>
-
| | |
You had quite a few syntax problems but I ironed them out. This will work cross-browser: -
<html>
-
<head>
-
<script>
-
function blah(){
-
var j=0;
-
var chkbox = document.createElement('input');
-
chkbox.type='checkbox';
-
chkbox.setAttribute('name','chkPortfolio');
-
chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
-
chkbox.setAttribute('value',"blah");
-
if( "True"=="True") {
-
chkbox.onclick= function() {alert('hi')};
-
}
-
else {
-
chkbox.checked=false;
-
}
-
chkbox.id = "chkPortfolio_" + (j+1);
-
chkbox.li_id = "trPortfolio_" + (j+1);
-
//var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
-
document.getElementById('body').appendChild(chkbox);
-
//listItem.appendChild(name);
-
//list.appendChild(listItem);
-
}
-
</script>
-
</head>
-
<body id="body" onload="blah()">
-
fdsfsdafasdfdsfsdasda
-
</body>
-
</html>
-
sorry i actually copied the wrong thing, but this helps. only problem is when... - if( "True"=="True") {
-
chkbox.onclick= function() {alert('hi')};
-
chkbox.checked=true;
-
}
i want to check the checkbox, but that doesn't seem to work. suggestions
| | Expert Mod 8TB |
sorry i actually copied the wrong thing, but this helps. only problem is when... - if( "True"=="True") {
-
chkbox.onclick= function() {alert('hi')};
-
chkbox.checked=true;
-
}
i want to check the checkbox, but that doesn't seem to work. suggestions
Why are you checking if "True" equals "True"? That will always return true so the code is redundant. It also means that the checkbox will never be unchecked.
should work as long as chkbox is a valid reference to a checkbox.
| | Expert 100+ |
Why are you checking if "True" equals "True"? That will always return true so the code is redundant. It also means that the checkbox will never be unchecked.
should work as long as chkbox is a valid reference to a checkbox.
That was my bad. I hard coded that condition because he was referencing some data that wasn't in his code snippet (and I couldn't debug). I'm sure when he uses the changes he'll put it back to the way it originally was.
| | Expert 100+ |
Why are you checking if "True" equals "True"? That will always return true so the code is redundant. It also means that the checkbox will never be unchecked.
should work as long as chkbox is a valid reference to a checkbox.
I can see the issue too. It works fine in FFox but IE doesn't show the check. I'm working on why. If anyone knows, do tell.
| | Expert Mod 8TB |
I can see the issue too. It works fine in FFox but IE doesn't show the check. I'm working on why. If anyone knows, do tell.
Most likely, the reason is that the checkbox must be contained within a form. So it should be appended to the body, rather it should be child to a form tag/element.
| | |
Most likely, the reason is that the checkbox must be contained within a form. So it should be appended to the body, rather it should be child to a form tag/element.
well basically i have a div called 'portfolio_box' within my form, where i append all the checkboxes i create. so i don't understand why it doesn't work in IE(6.0.2900) or FF(2.0.0.1) for me.
| | Expert Mod 8TB |
Then, in that case, post the entire code, or give a url to the page containing your code.
| | Expert Mod 8TB |
Most likely, the reason is that the checkbox must be contained within a form. So it should be appended to the body, rather it should be child to a form tag/element.
Mistake: the second sentence should be, So it should not be appended to the body, rather it should be a child element to a form/tag element.
I would've edited the original post, but it's already been quoted upon (noticed it too late).
| | | Mistake: the second sentence should be, So it should not be appended to the body, rather it should be a child element to a form/tag element.
I would've edited the original post, but it's already been quoted upon (noticed it too late).
This is all within an aspx web page, but there is a whole bunch of other stuff going on, so i made a quick sample for a simple page. The checkboxes are selected in FF , but not IE6...any ideas? I can't get either to be checked when I essentially have this in my aspx page. Thanks for the help. - <html>
-
<header>
-
<script language="javascript">
-
function loadPort() {
-
-
var portObj = document.getElementById('portfolio_box');
-
var list = document.createElement('ul');
-
list.setAttribute('id','chk_list');
-
portObj.appendChild(list);
-
for(j=0; j<10; j++)
-
{ var listItem = document.createElement('li');
-
listItem.setAttribute('id',"trPortfolio_" + (j+1));
-
var chkbox = document.createElement('input');
-
chkbox.setAttribute('type','checkbox');
-
chkbox.setAttribute('name',"nuts");
-
chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
-
chkbox.setAttribute('value',"nuts");
-
if("True"=="True") {
-
/* Nothing seems to work */
- chkbox.checked=1;
-
chkbox.checked=true;
-
chkbox.setAttribute('selected',true);
-
chkbox.setAttribute('selected','true');
-
}
-
else {
-
chkbox.checked=false;
-
}
-
chkbox.onclick= function() {
-
alert("nuts");
-
}
-
chkbox.id = "chkPortfolio_" + (j+1);
-
var name = document.createTextNode("test" + (j+1));
-
listItem.appendChild(chkbox);
-
listItem.appendChild(name);
-
list.appendChild(listItem);
-
}
-
-
portObj.appendChild(list);
-
}
-
</script>
-
-
</header>
-
-
-
<body>
-
-
-
<form id="Form1" method="post" >
-
<div id="message"></div>
-
<div class="item_list">
-
<div class="title">Portfolios <input id="checkIt" onclick="checkAll(this.checked,'chkPortfolio',60);" type="checkbox" checked></div>
-
<div id="portfolio_box"></div>
-
</div>
-
<input type="button" onclick="loadPort();" value="click me">
-
</form>
-
-
</body>
| | Expert Mod 8TB |
Ok, I figured it out.
IE, in its wisdom, stupidly ignores your checked setting until and unless you have appended the checkbox to something. If it's still free-standing, it just completely ignores it and sets it to (or keeps it) unchecked!
There are two ways around the problem. One is to set the checked property after you've appended the checkbox to the list-item. The second option is to use the defaultChecked property instead (thankfully, at least, IE recognises that!)
I got this info. from this site.
| | |
Of all the research I did, I could find just the hack but none explained as to why it wouldnt work with IE. Thanks to Acoder for detailed explanation.
| | Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
reply
views
Thread by starace |
last post: by
|
3 posts
views
Thread by Leo J. Hart IV |
last post: by
|
5 posts
views
Thread by Irfan Akram |
last post: by
|
3 posts
views
Thread by Colin |
last post: by
| | | |
reply
views
Thread by =?Utf-8?B?SWRlcm9ja3M=?= |
last post: by
| | | | | | | | | | | |