I’m 90% off the way there with this (I think?) just need a little help to get it over the line.
When a user clicks .foogallery-container h6 the h6 text is inserted into a form field, #field_iqmf9t. No issues there.
However what I want, is for that text to be removed from the form field if the button is clicked again (toggled). Here’s the jQuery I’ve thrown together, I’ll explain exactly what’s happening below.
jQuery(document).on('click', '.foogallery-container h6', function(e){
if (!jQuery(this).attr('selectoggle') || jQuery(this).attr('selectoggle') == 'off'){
jQuery(this).attr('selectoggle','on');
jQuery('#field_iqmf9t').append(jQuery(this).text() + ", ");
}
else if (jQuery(this).attr('selectoggle') == 'on'){
jQuery(this).attr('selectoggle','off');
jQuery('#field_iqmf9t').remove(jQuery(this).text() + ", ");
}
});
The text is inserted into the form field as intended, no probs there.
If I use .remove on the ‘elseif’ click 1 inserts the text, click 2 nothing happens (because selectoggle is in “on position”) then click 3 it inserts the text again (there are now two instances of the text)
If I use .empty on the ‘elseif’ click 1 inserts the text, click 2 removes the text (nice) however if I click multiple .foogallery-container h6 each will be inserted as intended, but then if I click any of the h6’s that have already been clicked, the toggle comes into effect and .empty removes everything in the form field. What I want it to do is ONLY remove the text of the h6 that is being clicked, not all h6’s.
Hope this makes sense. I’m sorry I can’t post an example, the site is being built locally.