Skip to content
Advertisement

JQuery, how to pass the slug variable

I’d like to know how I can pass the slug variable into JQuery/javascript.

I have a data-table with items, on each row there are two buttons, one (1) is using Django to create a product, the other (2) is supposed to use JQuery / JS to create a product.

To create a product with button 1 I find being straight forward and well explained.

I’d like to perform the same action on button 2, using JQuery/JS.

button 1

JavaScript

with urls path:

JavaScript

views.py

JavaScript

button 2

JavaScript

with urls path:

JavaScript

with js (truncated function)

JavaScript

Advertisement

Answer

With help of @31piy answer to this SO post: How to console.log value of a data-attribute in jquery?

To select all the elements which have data-group attribute, you can loop over the elements selected by the selector [data-group], and in the iteration, get the value of the attribute using the data method.

Please see the example below:

JavaScript

I figured out that the const url could be expressed as:

JavaScript

When logging it in the console, I will get the string I wanted

JavaScript

Thus, the complete code would become:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement