homepage Welcome to WebmasterWorld Guest from 54.166.113.249
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
How can I get this value into my JS code?
RickB



 
Msg#: 4610813 posted 12:35 am on Sep 19, 2013 (gmt 0)

Guys, I'm trying to get a value to populate my JS code dynamically. Here's an example.

I need to have a 17 digit VIN number like this "2T1BURHE6EC013995" to populate into my JS code for google adwords in this section "ecomm_prodid: 'REPLACE_WITH_VALUE',".

<script type="text/javascript">
var google_tag_params = {
ecomm_prodid: 'REPLACE_WITH_VALUE',
ecomm_pagetype: 'REPLACE_WITH_VALUE',
ecomm_totalvalue: 'REPLACE_WITH_VALUE',


I have a RegEX that can find the VIN number. var myRegxp = /[A-Z0-9]{17}/

My question is how can I get my VIN number to populate into my JS code dynamically. Can I just place my RegEx Var in and it pull it automatically?

Any pointers or help is greatly appreciated!

 

daveVk

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4610813 posted 1:50 am on Sep 19, 2013 (gmt 0)

You need to apply the RegEX to some text to get the VIN, where is this text coming from ?

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4610813 posted 2:07 am on Sep 19, 2013 (gmt 0)

Not sure I'd worry about the regex and trying to parse through the page to find a vin or anything like it seems you'd have to do to need a regex to find the info on the page -- Personally, I think I'd just put the vin in a <span> with an id and grab the value with JS.

<span id="vin_number">2T1BURHE6EC013995</span>

<script type="text/javascript">
var vin_num=document.getElementById('vin_number').innerHTML;

var google_tag_params = {
ecomm_prodid: vin_num,
ecomm_pagetype: 'REPLACE_WITH_VALUE',
ecomm_totalvalue: 'REPLACE_WITH_VALUE',

RickB



 
Msg#: 4610813 posted 3:01 am on Sep 19, 2013 (gmt 0)

Thank you guys soooo much for your replys. JD, I think your way would work the best for me in this scenario. Using your example, how would I extract only the VIN from this piece of code. Well, let me change that question. Using your example, I know how to extract the inner.html, but it will extract "Vin:xxxxxxxxxx".

<span class="value">6-Speed Automatic</span>
</li>
<li class="exteriorColor">
<strong class="title">
Ext. Color:
</strong>
<span class="value">White Diamond Tricoat</span>
</li>
<li class="vin">
<strong class="title">
VIN:
</strong>
<span class="value">1GYUKCEF6AR250229</span>
</li>
<li class="modelCode">
<strong class="title">
Model Code:
</strong>
<span class="value">6K10706</span>
</li>
<li class="stockNumber">
<strong class="title">
Stock #:
</strong>
<span class="value">00DG438A</span>
</li>
<li class="bodyStyle">
<strong class="title">
Bodystyle:

Can I strip the word "Vin:" and only get the 17 Chars? If I tell it to grab the inner.html, it will get "Vin:xxxxxxxxxxxx". I only need the number.

Also, if you need the full html, here's a page on the website that I manage.

[beamantoyota.com...]

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4610813 posted 3:47 am on Sep 19, 2013 (gmt 0)

It's really a simple change in your template:

<li class="vin">
<strong class="title">
VIN:
</strong>
<span class="value" id="vin_number">1GYUKCEF6AR250229</span>
</li>

<script>
// As low on the page as you can, but before the call for the ads
var vin_num=document.getElementById('vin_number').innerHTML;

var google_tag_params = {
ecomm_prodid: vin_num,
ecomm_pagetype: 'REPLACE_WITH_VALUE',
ecomm_totalvalue: 'REPLACE_WITH_VALUE'
}
</script>

In PHP you might have to do a bit more work, like:

$id_for_class='';
if($variable_for_the_li_class=='vin') {
$id_for_class=' id="vin_number"';
}

echo '<li class="'.$variable_for_the_li_class.'">
<strong class="title">
VIN:
</strong>
<span class="value"'.$id_for_class.'>1GYUKCEF6AR250229</span>
</li>';

daveVk

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4610813 posted 5:39 am on Sep 19, 2013 (gmt 0)

If changing the HTML being generated is an option then consider generating

<script>var vin_num="1GYUKCEF6AR250229";</script>

if NOT in jQuery

var vin_num = $('.vin').find('span').text();

Readie

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4610813 posted 12:48 pm on Sep 19, 2013 (gmt 0)

var vinNo = document.getElementsByClassName('vin')[0].getElementsByClassName('value')[0].innerHTML;

If you're not using jQuery and want to make no changes to your templates.

RickB



 
Msg#: 4610813 posted 4:36 pm on Sep 19, 2013 (gmt 0)

Thank you guys for your help. I'm very green with JS and I'm currently trying to test what you guys recommended. Our dealership pays a company to provide the website platform for us. I know that I can't change the template. As far as if they are using jQuery, there are a few functions on the page using jQuery at the bottom of the page. I don't know if that is affecting the section of the page I'm looking at.

RickB



 
Msg#: 4610813 posted 5:31 pm on Sep 20, 2013 (gmt 0)

Guys, I need you help again with testing. I'm trying to get the script to write it to the doc with the vin #. That way I know for sure that it's getting the right info and working. Right now, it's not working. What am I doing wrong?

<script>

var vin_num = document.getElementsByClassName('vin')[0].getElementsByClassName('value')[0].innerHTML;

document.write(vin_num);

</script>

RickB



 
Msg#: 4610813 posted 7:57 pm on Sep 20, 2013 (gmt 0)

Guys,

I apologize. It is working and working correctly. I was using a different program to test the code and program wasn't working correctly. I tried it on w3schools.com and it worked perfect and also in Dreamweaver. Thank you so much for your help.

daveVk

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4610813 posted 12:45 am on Sep 23, 2013 (gmt 0)

getElementsByClassName is relatively new to IE ( IE9 ) ensure you are using a library that supports it for older browsers.

[caniuse.com...]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved