cesy: "Cesy" - An old-fashioned quill and ink (Default)
Cesy ([personal profile] cesy) wrote in [site community profile] dw_dev_training2009-10-09 04:45 pm
Entry tags:

More Javascript help, please

I have a link, and when you click on it, it calls edit_tagcloud(tagname);.
I then have a function defined:
function edit_tagcloud(newtag)
{
if (! newtag) return;
var cur_taglist = document.getElementById('prop_taglist').value;
if (cur_taglist.length > 0) {
cur_taglist = cur_taglist + ", " + newtag;
} else {
cur_taglist = newtag;
}
document.getElementById('prop_taglist').value = cur_taglist;
}

What I'm aiming for is that tagname is added to the list already in prop_taglist, which is the id for the tags input box. However, the code above doesn't work - nothing happens when I click on the link. Can anyone tell me what I'm doing wrong, or give me any tips on how to debug Javascript like this?
janinedog: (Default)

[personal profile] janinedog 2009-10-09 04:52 pm (UTC)(link)
I don't see anything immediately wrong with what you have, but I'm not sure what the page itself looks like. To debug, Firebug sometimes helps by giving you more useful errors than the JS console in Firefox does. What I usually do, though, is just add in alert()'s at different points to see where it's getting to, and where it's not.

Okay, I thought of something that might make this fail...if prop_taglist is undef, there won't be a value on it. And actually, if it's defined but empty, then I think .value might give you undef instead of an empty string, which then isn't a valid string. So check for that.
serene: mailbox (Default)

[personal profile] serene 2009-10-09 05:12 pm (UTC)(link)
From my partner [livejournal.com profile] someotherguy: "The element ID cannot have an underscore in it. Underscores are not a valid character in CSS. Some browsers will recognize it and some won't."
janinedog: (Default)

[personal profile] janinedog 2009-10-09 05:24 pm (UTC)(link)
There are plenty of underscores in IDs in the code, so I'm pretty sure that's not the issue here...but it's a good point. I think they're only not supported in old browsers though, none of which DW supports.
pne: A picture of a plush toy, halfway between a duck and a platypus, with a green body and a yellow bill and feet. (Default)

[personal profile] pne 2009-10-11 06:08 pm (UTC)(link)
Underscores are not a valid character in CSS.

Reference, please?

http://www.w3.org/TR/CSS21/syndata.html#syntax defines an identifier of starting with an optional '-', followed by a "nmstart" character and one or more "nmchar" characters, and the underscore is explicitly included in both "nmstart" and "nmchar".
pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)

[personal profile] pauamma 2009-10-09 05:38 pm (UTC)(link)
Two random shots:
- Is edit_tagcloud called at all?
- Is that statement:
if (! newtag) return;
returning prematurely?