jQuery on click, build array of data attributes -
I've found a list that gives me an idea of parenting on anchor click and get data-brand attribute And store it in an array and add it to the array on the click of any other item. I also want that if you have already removed an item, then it has been removed.
Is it quite straight forward?
HTML
& lt; Div class = "search-data__filter" & gt; & Lt; Ul & gt; & Lt; Li data-brand = "prada" & gt; & Lt; A href = "#" square = "chkbox" & gt; Prada & lt; / A & gt; & Lt; / Li & gt; & Lt; Li data-brand = "oakley" & gt; & Lt; A href = "#" class = "chkbox" & gt; Oakley & lt; / A & gt; & Lt; / Li & gt; & Lt; Li data-brand = "re-ban" & gt; & Lt; A href = "#" square = "chkbox" & gt; Ray-Ban & lt; / A & gt; & Lt; / Li & gt; & Lt; Li data-brand = "nike" & gt; & Lt; A href = "#" square = "chkbox" & gt; Nike & lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Div & gt; so far:
$ (document) .ready (function () {checkbox = $ (".chkbox"); checkbox.one (" Click ", function (e) {$ (this) .toggleClass (" checked "); var ar = $ (this) .Parent (" li "). Map (function () {return $ (this) Attr ('data-brand');}); console.log (ar); e.preventDefault ();});}); My log is only collecting one data-attribute and I think it can be because I'm essentially mapping parents who clicked Used to be? How can I expand on this Can not work.
You really have to save the stuff array:
$ ( Document) .ready (function () {var checkbox = $ (".chkbox"); var brand = []; // new array checkbox.on ("click", (e) "$ (this) .toggleClass Parent ("li"). Attr ('data-brand'); // Inexperienced Console.log (brand); e.preventDefault () ;});}); It's simple, just connects it. Now, we need to check whether it is before adding it, and if so, then delete it:
$ (document) .ready (function () {var checkbox = $ (". Chkbox" brand "= []; // new array checkbox.on (" click ", function (e) {$ (this) .toggleClass (" checked "); var b = $ ( This) .Parent "li"). Etr ('data-brand'); // so we do not have to write it every time if (brand. Index (b)! = - 1) {brand.plice (brands.indexOf () B), 1);} and {Push (B) brands;} console.log (brand); E.Pravant D Olt ();});});
Comments
Post a Comment