PDA

View Full Version : Adding Tags via Javascript



foxboyprower
08-05-2015, 02:36 PM
I'm making a firefox addon that'll help users post journals on multiple websites at once. I won't bore you with the details.
But I've run into an issue on weasyl about adding tags.

This is the code I was using to put tags into the tag box:
______________________

var cargo = {
tags: ["These","tags","were","added","with","javascript"],
};

var z = document.querySelector("#submit-journal-content #submit-form .tagging .listbuilder");
var tagPadding = ['<li class="listbuilder-entry" unselectable="on"><span class="listbuilder-entry-text">','</span><a class="listbuilder-entry-remove" href="#" role="button" title="Remove sampletag from the list."></a>'];
var package = "";
for(var i = 0; i < cargo.tags.length; i = i + 1)
{
package = tagPadding[0]+cargo.tags[i]+tagPadding[1];
console.log(package);
z.innerHTML = z.innerHTML + package;
}

______________________

At first glance, this seems to work, but when the journal is submitted, there are no tags on the journal.
Any idea on what's going wrong?

Socks the Fox
08-06-2015, 10:56 AM
Are you also adding the tags to the `tags-textarea` <textarea>? If not, they need to be space-delimited (i.e. `journal problem tagging`). That's where the site gets the actual list of tags and the site's JS just fancifies that with the <li>s.

foxboyprower
08-06-2015, 03:05 PM
Alright, thanks. I forgot stuff can be displayed without the site storing it. That tends to happen on the fancier sites. Here's my fixed code for all those people of the future having this issue.
______________________

var cargo = {
tags: ["These","tags","were","added","with","javascript"],
};

//get tag area
var WStagBox = document.querySelector("#submit-journal-content #submit-form .tagging .listbuilder");
var WStags = document.querySelector("#tags-textarea");

//set tag value
var tagPadding = ['<li class="listbuilder-entry" unselectable="on"><span class="listbuilder-entry-text">','</span><a class="listbuilder-entry-remove" href="#" role="button" title="Remove sampletag from the list."></a>'];
var Tags="";
for(var i = 0; i < cargo.tags.length; i = i + 1)
{
Tags = tagPadding[0]+cargo.tags[i]+tagPadding[1];
WStags.value = WStags.value+cargo.tags[i] + ' ';
WStagBox.innerHTML = WStagBox.innerHTML + Tags;
}
WStags.value.substring(0, WStags.value.length - 1);

console.log(WStags.value);