Need some Javascript help
16-02-2005, 17:42
|
#1
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 46
Posts: 6,343
|
Need some Javascript help
I'm confused, what's supposed to happen with this page is:
You click on the "Done" link and it marks the task as done, that's fine with the first one but when you go to the next one you need to click it 2 or 3 times before it will work. 
It does the same in IE and Mozilla browsers...
I've attached the entire page as .txt which you'll need to rename to .html, the javascript is being generated server-side BTW.
Thanks in advance to anyone can spot the error.
|
|
|
16-02-2005, 18:10
|
#2
|
|
Inactive
Join Date: Jul 2003
Posts: 1,395
|
Re: Need some Javascript help
It's the 'done' flip-flop variable, you need to have a flip-flop for each of the tasks, probably best to use an array here. Basically, when you click a done link, the value of 'done' flips over so you have to click twice the next time, once to get it back to the orignal status then again to mark as done.
|
|
|
16-02-2005, 18:19
|
#3
|
|
Inactive
Join Date: Mar 2004
Location: Glasgow, Scotland
Services: anything for a new job
Posts: 4,165
|
Re: Need some Javascript help
I may be wrong here, but your execute function takes an if else, and you are assigning 1 to 0 and 0 to 1, this may be your problem, I have noticed that even the third element of Done works the exact same, so this may show why, you are haveing this problem, thats the only thing I can see.
ik
|
|
|
16-02-2005, 19:19
|
#4
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 46
Posts: 6,343
|
Re: Need some Javascript help
Thanks guys.
So basically I should do this?
Code:
function execute(id) {
var taskitem = document.getElementById("taskitem_section" + id);
var msg_done = document.getElementById("msg_done" + id);
var title = document.getElementById("title" + id);
var check = document.getElementById("check" + id);
if (msg_done.style.display="none") {
taskitem.style.opacity="0.3";
title.style.textDecoration="line-through";
msg_done.style.display="inline";
}
else {
taskitem.style.opacity="1";
title.style.textDecoration="none";
msg_done.style.display="none";
}
|
|
|
16-02-2005, 19:38
|
#5
|
|
Inactive
Join Date: Jul 2003
Posts: 1,395
|
Re: Need some Javascript help
Quote:
|
Originally Posted by Richard M
Thanks guys.
So basically I should do this?
|
I liked my way better (I attatched my version to the last post)  but yes, as long as you are checking on a per document basis it will be fine. Your way is probably less error prone though.
|
|
|
16-02-2005, 19:45
|
#6
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 46
Posts: 6,343
|
Re: Need some Javascript help
Sorry I didn't even notice you attached the page. 
It works now! 
Thanks.
|
|
|
22-02-2005, 11:47
|
#7
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 46
Posts: 6,343
|
Re: Need some Javascript help
I need help with a related thing, I can't figure out how to pass the contents of the textarea into the script.
HTML:
Quote:
<form onsubmit="return false;"><strong>Comments:</strong><br />
<textarea rows="8" cols="30" style="width: 300px;" name="comments1" id="comments1"></textarea><br />
<input class="execute" type="submit" value="-->" name="execute" style="width: 300px; height: 30px;" onclick="post_comments(1,commentpost1); " />
|
Script:
Quote:
comments1 = document.getElementById("comments1");
commentpost1 = comments1.value;
function post_comments(id,text) {
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
post_str1 = "post_comments.php?record=";
post_str2 = "&comments=";
post_str = post_str1 + id + post_str2 + text;
xmlhttp.open("POST", post_str, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var comments = document.getElementById("commentresult" + id);
comments.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null)
}
|
If I look at the contents of "post_str" it gives this so the comments field is not being passed to it:
post_comments.php?record=1&comments=
I'm sure there's something simple that I've overlooked.
|
|
|
22-02-2005, 12:07
|
#8
|
|
Inactive
Join Date: Jul 2003
Posts: 1,395
|
Re: Need some Javascript help
At a quick glance it looks okay, the only thing that I can suggest is:
a) Close the form tag (sorry if you have, it's just not in the section you posted).
b) Give the form a name/id
|
|
|
22-02-2005, 12:30
|
#9
|
|
Inactive
Join Date: Jun 2003
Age: 44
Posts: 14,750
|
Re: Need some Javascript help
Well, if you know the ID you don't need to pass the text in, it is unnesscessary. I tried to fix what you said, but getting the comments1 value outside the function makes javascript throw a hissy fit. It needs a document.getElementById around it anyway. Outside the function, it doesn't work, inside, it does. I can only see that there is a scope issue there somewhere.
I would just do:
HTML section:
Code:
onclick="post_comments(1);
And in the script section:
Code:
function post_comments(id) {
commentpost1= document.getElementById('comments' + id).value;
...
}
Try that, it should work for you. That seems to work for me anyway.
|
|
|
22-02-2005, 13:56
|
#10
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 46
Posts: 6,343
|
Re: Need some Javascript help
Nice one! That works, thanks.
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +1. The time now is 16:13.
|