简体   繁体   English

从事件单击按钮获取 InertText(对于 GTM)

[英]Get InnertText from event click button (for GTM)

I tried to get innerText from a click button with js event for exemple :我尝试从带有 js 事件的单击按钮获取innerText,例如:

 <label class="sc-AxirZ haYsgf"> <span>brouh</span> <span></span> <input type="radio" name="bruh" value="bruh1"> </label> <label class="sc-AxirZ haYsgf"> <span>test</span> <span></span> <input type="radio" name="bruh1" value="bruh2"> </label> <label class="sc-AxirZ haYsgf"> <span>test2</span> <span></span> <input type="radio" name="bruh3" value="bruh4"> </label>

I would like to get the value of the button when I clicked on it.我想在单击按钮时获取按钮的值。 (get "moins de 20 ans" if I clicked on it) The only one prob it's, I've more than one button with the same class (如果我点击它,就会得到“moins de 20 ans”)唯一的一个问题,我有不止一个按钮具有相同的类别

Can you help me, please?你能帮我吗? I try to do this for Google Tag Manager to get values.我尝试为 Google Tag Manager 执行此操作以获取值。

Thanks a lot非常感谢

Attach event listeners to the radio buttons and call a function when they change.将事件侦听器附加到单选按钮并在它们更改时调用函数。

 const radios = document.querySelectorAll('input[type="radio"]'); radios.forEach(radio => radio.addEventListener('change', handleChange, false)); function handleChange(e) { console.log(e.target.value); }
 <label class="sc-AxirZ haYsgf"><span>Moins de 25 ans</span><span></span><input type="radio" name="BAS110Q" value="BAS110R01"></label> <label class="sc-AxirZ haYsgf"><span>Moins de 20 ans</span><span></span><input type="radio" name="BAS110Q" value="BAS110R02"></label> <label class="sc-AxirZ haYsgf"><span>Moins de 29 ans</span><span></span><input type="radio" name="BAS110Q" value="BAS110R03"></label>

Alternatively you can wrap your radio buttons in a container and apply the event listener to that element.或者,您可以将单选按钮包装在容器中并将事件侦听器应用于该元素。

 const container = document.querySelector('#container'); container.addEventListener('change', handleChange, false); function handleChange(e) { console.log(e.target.value); }
 <div id="container"> <label class="sc-AxirZ haYsgf"><span>Moins de 25 ans</span><span></span><input type="radio" name="BAS110Q" value="BAS110R01"></label> <label class="sc-AxirZ haYsgf"><span>Moins de 20 ans</span><span></span><input type="radio" name="BAS110Q" value="BAS110R02"></label> <label class="sc-AxirZ haYsgf"><span>Moins de 29 ans</span><span></span><input type="radio" name="BAS110Q" value="BAS110R03"></label> </div>

Additional documentation附加文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM