0

I Have this script below that suppose to get values from input fields and then add them in a href link. but i keep getting this error in console:

Uncaught Error: Syntax error, unrecognized expression: ?add-to-cart=7690&variation_id=8498&attribute_pa_color=gold

Here is the script:

    jQuery(document).on("click", ".variations_form.cart .variation_buttons_wrapper a", function(){
    // Get all the values needed for the URL
    var add_to_cart = jQuery(this).parent().parent().parent().parent().parent().parent().find( 'input[name="add-to-cart"]' ).val();
    var variation_id = jQuery(this).parent().parent().parent().parent().parent().parent().find( 'input[name="variation_id"]' ).val();    
    var pa_color = jQuery(this).attr("id");
    setTimeout(function(){
    // Craft the URL
    var link = jQuery("?add-to-cart=" + add_to_cart + "&variation_id=" + variation_id + "&attribute_pa_color=" + pa_color);
    alert(link);
    }, 500); 
   });

Any ideas??

4 Answers 4

1

You are using the jQuery object the wrong way.
If you are trying to craft a link do it this way:

var link = "?add-to-cart=" + add_to_cart +
"&variation_id=" + variation_id +
"&attribute_pa_color=" + pa_color;  

No need for the jQuery object here.

Sign up to request clarification or add additional context in comments.

1 Comment

I will but apparently i have to wait 5 more minutes.
1

There is no need for the jQuery object when crafting the URL. Simply use it without the object.

var link = "?add-to-cart=" + add_to_cart + "&variation_id=" + variation_id + "&attribute_pa_color=" + pa_color;

Comments

1

This is because you are passing querystring as jQuery selector. You need to create a valid selector for the same.

To add to href link, first select the link and get the URL

var link = $("#link").attr("href");

Then add the querystring to it

link += "?add-to-cart=" + add_to_cart + "&variation_id=" + variation_id + "&attribute_pa_color=" + pa_color;

Suggestion:

If you are using a form, a shortcut way to get all fields as querystring is using jQuery serialize method. Example:

var queryString = $("#form").serialize();

Make sure all your form fields have name attributes.

Few words of wisdom: Avoid to many parent() calls to reach the target element. You code will break if the DOM changes and nesting levels increases or decreases.

Comments

0

jQuery("?add-to-cart="...) is not a valid DOM selector, hence the error. You need to leave it as a string. Or even better, use jQuery.param:

var link = jQuery.param({
  'add-to-cart': add_to_cart,
  'variation_id': variation_id 
})

BTW, instead of doing this:

var add_to_cart = jQuery(this).parent().parent().parent().parent().parent().parent().find( 'input[name="add-to-cart"]' ).val();

You could do something like this:

jQuery(this).closest('.variations_form').find('input...')

(replace variations_form with whatever parent class name you have)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.