3

I'm working on a piece of JavaScript. I'm completely new to JS, But I have a group of checkboxes part of the domainchkb class and with the name, all with different values. I am trying to group those into an array to format into JSON. When executing the code the debugger stops at return this.parent().text();. What am I doing wrong here? Any help would be appreciated.

function format4Cart(){
    var domains;

    var values = $('input[name=domainchkb]:checked').map(function(){
            return this.parent().text();
    }).get();

    domains = "{\"type\" : \"single\", \"data\" : [";

    $.each(values, function(domainIndex,selected_domain){
            domains += "{\"id\" : \""+selected_domain+"\", \"domain\" : \""+whois_response.data.domain+"\"},";
    });
    $(domains).text().replace(/(\s+)?.$/,"");
    domains += "]}";
    domains=encodeURIComponent(domains);
    ajaxAdd2Cart(domains);

}

2 Answers 2

1

You are trying to call a jQuery method, .parent(), on a DOM element. In order to use a jQuery method on the element, you need to wrap it in a jQuery call:

var values = $('input[name=domainchkb]:checked').map(function(){ 
        return $(this).parent().text(); 
}).get(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Gilly and everyone else for their help.
1

If you're using the debugger already just look at it see what values are there for this and other variables. Might be something you do not expect. I think the issue is that you're trying to use this while you should use function parameter:

var values = $('input[name=domainchkb]:checked').map(function(checkbox){
        return checkbox.parent().text();
}).get();    

2 Comments

Thank you. I will try it now. Basicly I'm checking if a domain is registered and if not allowing the person to select a group of them and then send it to a script to generate products and add them to a magento cart for check out. Then link them to a reseller API once a IPN has been received purchase the domains from reseller and send the information to them in a email.
It still exits the routine after the first pass just like before. hmm.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.