Expand my Community achievements bar.

Join us January 15th for an AMA with Champion Achaia Walton, who will be talking about her article on Event-Based Reporting and Measuring Content Groups!
SOLVED

How do you extract data from a dynamic Id -> "uid" to be capture in an event variable

Avatar

Level 3
I need to pull the data from an input box. The problem is that the id associated with the input box is dynamic.. If you look at the script below you will see the following ->"uid_loanAmount_16597654821991736" . However that number changes each time a visitor to the site. so next time it could be "uid_loanAmount_234568385923" or some other random number. So when I write the script to pull data from this id I can't include the number portion of the id. Does anyone have any suggestion? 
 
<input id="uid_loanAmount_16597654821991736" type="number" name="loanAmount" class="form-control input-large input-small no-arrows number" min="0" step="1" aria-describedby="uid_loanAmount_16597654821991736-error" aria-required="true" aria-invalid="false">
 
I want to do soemthing like the following script to extract the imput in this field.
$("#uid_loanAmount_").mouseleave(function(){
 var loanAmount = $this("#uid_loanAmount_") ;
if (loanAmount.length){
s.event17 = loanAmount_ }
else (// do something
     return false;
});
1 Accepted Solution

Avatar

Correct answer by
Level 3

arob87 wrote...

  1. $("input[name='loanAmount']").mouseleave(function(){
 
  1. var loanAmount = $("input[name='loanAmount']").val();
  2. if (loanAmount.length){
  3. s.events= "event100:"+loanAmount;}
  4. else {
  5. return false}
  6. });

You need to add a colon for event serialization and take the variable name out of quotes so that the variable passes through

 

Thank you, this worked perfectly.....smileyyes

View solution in original post

9 Replies

Avatar

Level 7

assume you're using jQuery from the above and that name="loanAmount" is always consistent so could you not use the following selector?

$("[name='loanAmount']").....

Avatar

Level 3

arob87 wrote...

assume you're using jQuery from the above and that name="loanAmount" is always consistent so could you not use the following selector?

$("[name='loanAmount']").....

 

 

 

I guess I could. I'm not very good at Jquery, so could you elaborate a little bit more with the code?  

Avatar

Level 3

OK I see what you are saying. That would allow me identify the element, however how do I take care of this part of the code

 var loanAmount = $this("#uid_loanAmount_") ; This is where the data is being stored. So how do I pull it from here. I'm not really good with HTML, so unless you are saying that the input value is some how stored within all parts of the element. I'm not sure how I can pull the input data from the element meaning I could do this  -> var loanAmount =$this(name='loanAmount') and it would extract the input value.. lets say its $305,000.. 

Avatar

Level 3

Darius Dempsey wrote...

arob87 wrote...

assume you're using jQuery from the above and that name="loanAmount" is always consistent so could you not use the following selector?

$("[name='loanAmount']").....

 

 

 

I guess I could. I'm not very good at Jquery, so could you elaborate a little bit more with the code?  

 

Could I do something like this and use a wild card in place of the id #

$("[name='loanAmount]").mouseleave(function(){
var loanAmount = $this("#uid_loanAmount_"); <--
if (loanAmount.length){
s.evar17 = loanAmount_ }
else (// do something
return false;
});

Avatar

Level 7

I'm also assuming you don't have DTM and this is going directly on the page so you'd have something like:

$("input[name='loanAmount']").mouseleave(function(){ var loanAmount = $("input[name='loanAmount']").val(); if (loanAmount.length){ s.eVar17 = loanAmount } else { return false} });

Avatar

Level 3

Actually I am using DTM, but I cannot use the CSS Selector on this page. But I am using the Custom page Code in DTM to implement this.

Avatar

Level 3

so this script works

$("input[name='loanAmount']").mouseleave(function(){ var loanAmount = $("input[name='loanAmount']").val(); if (loanAmount.length){ s.events= "event100 = loanAmount "} else { return false} });

but the s.events portion does not capture the numerical value stored in the variable loanAmount.. I am not sure how to pass it to an s.event variable. I did verify that the numerical value is being captured in this script because I passed it to an s.prop variable to test it. and it pulls the acutally numerical value that is typed into the input box. But it does not pass it to the s.event variable. what I get instead is the literal text "loanAmount" <- this is what shows up in the s.event variable. I also set the s.event to currency. So can anyone suggest how to pass the actual numerical integer to the s.event variable.  

Thanks

Avatar

Level 7
$("input[name='loanAmount']").mouseleave(function(){
var loanAmount = $("input[name='loanAmount']").val(); if (loanAmount.length){ s.events= "event100:"+loanAmount;} else { return false} });

You need to add a colon for event serialization and take the variable name out of quotes so that the variable passes through

Avatar

Correct answer by
Level 3

arob87 wrote...

  1. $("input[name='loanAmount']").mouseleave(function(){
 
  1. var loanAmount = $("input[name='loanAmount']").val();
  2. if (loanAmount.length){
  3. s.events= "event100:"+loanAmount;}
  4. else {
  5. return false}
  6. });

You need to add a colon for event serialization and take the variable name out of quotes so that the variable passes through

 

Thank you, this worked perfectly.....smileyyes