Extract field value in javascript of web page

This script is present in the html web page.

jQuery(function($) {
new Shopify.OptionSelectors('productSelect', {
  product: {
    "id":626976579,
    "title":"changedMacbook Air",
    "handle":"macbook-air",
    "description":"\u003cp\u003elightweight \u003c\/p\u003e\n\u003cp\u003eawesome performance\u003c\/p\u003e\n\u003cp\u003ewow display\u003c\/p\u003e\nHello World626976579[\"78000.00\"] [\"78000.00\"]\\n[\"78000.00\"] [\"78000.00\"]\u003cbr\u003e[\"78000.00\"]\u003cbr\u003e626976579\u003cbr\u003e626976579\u003cbr\u003e626976579",
    "published_at":"2015-05-25T02:39:00-04:00",
    "created_at":"2015-05-25T02:40:44-04:00",
    "vendor":"Test_Store",
    "type":"Computers",
    "tags":[],
    "price":7800000,
    "price_min":7800000,
    "price_max":7800000,
    "available":true,
    "price_varies":false,
    "compare_at_price":null,
    "compare_at_price_min":0,
    "compare_at_price_max":0,
    "compare_at_price_varies":false,
    "variants":[{"id":1754837635,"title":"Default Title","options":["Default Title"],"option1":"Default Title","option2":null,"option3":null,"price":7800000,"weight":800,"compare_at_price":null,"inventory_quantity":-29,"inventory_management":null,"inventory_policy":"deny","available":true,"sku":"20","requires_shipping":true,"taxable":true,"barcode":"","featured_image":null}],"images":["\/\/cdn.shopify.com\/s\/files\/1\/0876\/1234\/products\/overview_wireless_hero_enhanced.png?v=1432536113"],"featured_image":"\/\/cdn.shopify.com\/s\/files\/1\/0876\/1234\/products\/overview_wireless_hero_enhanced.png?v=1432536113","options":["Title"],"content":"\u003cp\u003elightweight \u003c\/p\u003e\n\u003cp\u003eawesome performance\u003c\/p\u003e\n\u003cp\u003ewow display\u003c\/p\u003e\nHello World626976579[\"78000.00\"] [\"78000.00\"]\\n[\"78000.00\"] [\"78000.00\"]\u003cbr\u003e[\"78000.00\"]\u003cbr\u003e626976579\u003cbr\u003e626976579\u003cbr\u003e626976579"},
    onVariantSelected: selectCallback,
    enableHistoryState: true
});

      

How do I access the value of the "title" field, which is "changedMacbook Air" here through my own JavaScript?

Thanks in advance.

+3


source to share


2 answers


I don't know if this will work, but try

var myProduct = new Shopify.OptionSelectors('productSelect', {
....
})

      

then try



console.log(myProduct)

      

or you can try this:

$(document).ready(function(e){
    console.log(document.title);
})

      

+1


source


I think you might have to go through a callback.

$('#productSelect').on('change', function(e) { 
    var t = e.target || e.srcElement,
        title = t.title.value;
            selectCallback(title);
            break;
        }
    }
 });


//If you want to trigger it right away for some reason, just use this...
$("#productSelect").change();

      

Look for code for option_selection.js in your product template or somewhere in your snippets, assets, or templates. See this fiddle for an example of what the code looks like. option_selection.js

You can also check this link by setting onchange event in product variants. I'm not sure what your ultimate goal is, but I can see that you are working with product variants and variants, so it might be helpful.



You can change option_selection.js if you need, but most likely you just need jquery in your document.ready document.

Here's an example from option_selection.js that builds the names of each selector. Although you probably don't need to change this, see the link above.

Shopify.OptionSelectors.prototype.buildSelectors = function() {
    for (var t = 0; t < this.product.optionNames().length; t++) {
        var e = new Shopify.SingleOptionSelector(this, t, this.product.optionNames()[t], this.product.optionValues(t));
        e.element.disabled = !1, this.selectors.push(e)
    }
    var o = this.selectorDivClass,
        i = this.product.optionNames(),
        r = Shopify.map(this.selectors, function(t) {
            var e = document.createElement("div");
            if (e.setAttribute("class", o), i.length > 1) {
                var r = document.createElement("label");
                r.htmlFor = t.element.id, r.innerHTML = t.name, e.appendChild(r)
            }
            return e.appendChild(t.element), e
        });
    return r
},

      

0


source







All Articles