Syntax for pushing a value to GTM DataLayer - javascript

Please advise on how to push to GTM dataLayer the value below step by step (I'm not a developer).
document.getElementById("mytdId").innerText
Thanks a lot.

ok i found a way around which works , here the snippet :
Hi
function (){
var topMenu = document.getElementById("dm1m0i1tdT").innerText;
return(topMenu);
}
To improve the syntax you suggest an array but don't know how to handle that in this case here. Any suggestion ?

Related

How to find a dynamic node class by pattern in JavaScript

I'm working on this WP plugin and I've been trying to get the ID of a custom post kinda thing that is declared in the body class on each page. So it goes like this;
<body class="(Bunch of other classes) ld-courses-1731-parent">
I'm trying to get the number 1731 in my JS function but the number is dynamic so I need to some regex matching with the string pattern.
Pattern: ld-courses-*INT VALUE*-parent
how can I do this with JS? Any help is much appreciated thank you so much.
You can use match if thats the only class in your body:
var classList = document.getElementsByTagName("body")[0].classList;
[...classList].forEach(function(thisClass) {
if (/ld-courses-\b/.test(thisClass)) {
var id = thisClass.match(/\d/g);
console.log(id.join(""));
}
});
<body class="another-class-before another-class-12-hasnum ld-courses-1731-parent another-class-12-hasnumaswell another-class-after">
</body>
Hi Laclogan in regards to your question yes it's possible for sure.
Correct me if i'm wrong but the number comes probably if it's changing for each post directly from the url.
The following site explains if this is the case how to get that number from the url.
https://www.sitepoint.com/get-url-parameters-with-javascript/
In addition you can use the function concat to combine the strings see this site for an example hope it helps.
https://www.w3schools.com/jsref/jsref_concat_string.asp
Could you confirm for me that the number is always present in the url or if this is not the case?

Why isn't my array index displaying?

This seems so simple and I honestly can't see why this isn't working. Looked at other answers on here and mine still isn't working.
var randomArray = new Array();
randomArray[0] = 859;
alert(randomArray[0]);
How come nothing happens? It's probably an easy answer but I can't work it out.
Also, I'm trying to work out how to put a random number in the array so rather than putting 859 in index 0 lets say I want to put a random number between 1 and 20 in, so far I've got this but thats not working either
randomArray[1]=Math.floor(Math.random()*3);
EDIT: the .toString on alert seemed to fix it, thanks guys, knew it would be something small!
Is your javascript being referenced properly in a script tag with the correct type set?
<script type="text/javascript" ...> </script>
If not, it's fully possible your browser is simply ignoring it.
This is a wild guess, because your question doesn't contain enough information to know for sure, but make sure that your code is in a <script> block that looks like this:
<script>
// your code
</script>
Get rid of any "type" or "language" attribute on the tag; they're not needed and they're a source of error. If the "type" value is mispelled, then the browser will completely ignore the code.
Try calling .ToString() on your array property.
alert(randomArray[0].toString());

How to access to wrapped object property in nodejs addon

I have the following code in javascript:
var c = new addon.Component();
c.ComponentLength = 3
How should i build my addon, so i can do the previous code? I've already followed the tutorials in http://nodejs.org/api/addons.html , but i'm stuck here.
Is this possible? Does anyone have a solution?
Thanks in advance.
The solution is on the v8.h, search for SetAccessor , which "Sets an accessor on the object template.".
An example: http://syskall.com/how-to-write-your-own-native-nodejs-extension/

passing form object and a variable at the same time in jquery post

i know this question may be a little out of curiosity but please help me understand because i have done research on this already but may needed an explanation since it wasn't clear enough
is it possible to pass a form object and a variable at the same time in jquery post
for example
var review=$('#review').val();
var post_url="url.com";
$.post(post_url,{'review':review, $('#registration_form').serialize()}, function(returned_form)
{
$('#show_items').html(returned_form);
});
can the above code work well
Yes, you can pass multiple values. You just need to name each one:
var review=$('#review').val();
var post_url="url.com";
$.post(post_url,
{
'review': review,
'registrationForm' : $('#registration_form').serialize()
},
function(returned_form) {
$('#show_items').html(returned_form);
}
);

jQuery validate, can't get it to work

OK, beating my head against the Javascript/jQuery wall over here,
here is the code, which I simply can't get to work properly, any help is highly appreciated!
Especially this bugs me, changing row 60 from
c.gbForm.validator = c.dom.gbForm.validate(c.gbForm.validator); to
c.gbForm.validator = $("#gbForm").validate(c.gbForm.validator);
and row 61 from
c.dom.gbForm.unbind('submit').submit(c.gbForm.doAdd); to
$("#gbForm").unbind('submit').submit(c.gbForm.doAdd);
makes it kinda work, except then I get
this[0] is undefined error which I think is the jQuery validate plugin but I simply can't locate the exact spot at fault... So any hints/pointers to why the whole "var c" business isn't working and the same for the "this[0]" part would be awesome!
Thanks for any assistance!
John
Yes, here are a few things to look at
c.gbForm.validator = $("#gbForm").validate(c.gbForm.validator);
here you are referencing c.gbForm.validator before it is set (assuming this is the first assignment to c.gbForm.validator).
try this.
c.gbForm.validator = $("#gbForm");
c.gbForm.validator = $("#gbForm").validate(c.gbForm.validator);
also, why do you call c.doc.gbForm in one spot and just c.gbForm in another?
and as the comment says, validation should be as simple as $("gbForm").validate();

Categories