1

Take a look at my code :

// is_array function
function is_array(input){ return typeof(input)=='object'&&(input instanceof Array); }

// Check if cos_in is an array. If is not, create him
if(!is_array(cos_in))
{
    var cos_in = new Array();
}

// Onclick function
function cos(pret,box,configuratie)
{
    // Create a value (is different on every click; using different box)
    cos_in[box] = box + '|||' + pret + '|||' + configuratie + '||||';

    // Insert values from array in some div with #cos id
    $("#cos").html(cos_in.join('||||'));
}

My problem is that the div with id #cos has from start value "test-empty", and for each time onclick function is executed, the div should have value from function. But is returns an empty div.

Some help please?

5
  • 1
    Could you please provide a jsfiddle.net demo and an example of your input and output (and the desired output)? Otherwise it's very difficult to understand what the problem is. The more information you provide the easier it is for us to help you and we can give you better solutions. Thank you! Commented Jan 28, 2013 at 21:46
  • Of course : jsfiddle.net/EgVHL Commented Jan 28, 2013 at 21:58
  • Ah ok.... the problem is that box is a string and arrays only work with numeric keys. Hence cos_in.join(...) returns an empty string. I'm not quite sure what's the purpose of the function, so I don't know what advice to give. If you really need to join all elements in the "array", you have to use an object and iterate and join the values manually. Commented Jan 28, 2013 at 22:00
  • ooo, i suposed the keys from array works with string to. In php works. Thanks. I find this subject : stackoverflow.com/questions/1384746/… and yes, seems like i need to use Object and not Array. But stil doesen't work :( jsfiddle.net/EgVHL/1 Commented Jan 28, 2013 at 22:10
  • @ZadyPenelopa: It doesn't work because there's nothing like Object.prototype.join. You cannot use an array-specific function which uses the length attribute and integer indices on an object. Commented Jan 28, 2013 at 22:19

2 Answers 2

0

Although this code can be improved a lot I tried to fix your first immediate problem here.

Do you want to append the result every time you click? Where is the join for? Are you trying to join the keys or the values? I assume for now you want the value and not the key.

window.cos_in = window.cos_in && window.cos_in instanceof Array ? window.cos_in : []

// Onclick function
function cos(pret,box,configuratie)
{
    // Create a value (is different on every click; using different box)
    cos_in.push(box + '|||' + pret + '|||' + configuratie + '||||');

    // Insert values from array in some div with #cos id
    $("#cos").html(cos_in.join('||||'));
}

Let me iterate a bit to get to something readable/understandable.


Here is a cleaner example of what you're doing. To improve it more I need to know where you're going with your links and parameters.

var cos = (function (cos_in) {

    return function cos(pret, box, configuratie) {
        // Create a value (is different on every click; using different box)
        cos_in.push(box + '|||' + pret + '|||' + configuratie + '||||');

        // Insert values from array in some div with #cos id
        $("#cos").text(cos_in.join('||||'));
    };

}([]));

Here is an example of an object version instead of an array...

var cos = (function (cos_in) {

    return function cos(pret, box, configuratie) {
        // Create a value (is different on every click; using different box)
        cos_in[box] = (box + '|||' + pret + '|||' + configuratie + '||||');

        // Insert values from array in some div with #cos id
        $("#cos").text(Object.keys(cos_in).join('||||'));
    };

}({}));
Sign up to request clarification or add additional context in comments.

Comments

0

This is a simple wrapper you could use:

function join(input, str) {
    if(typeof(input) === 'object') {
        if(input instanceof Array) {
            return input.join(str);
        } else {
            var tmp = [];
            for(var x in input) {
                if(input.hasOwnProperty(x)) {
                    tmp.push(input[x]);
                }
            }
            return tmp.join(str);
        }
    }
    return input;
}

/* ... */

$("#cos").html( join(cos_in, '||||') );

However, you really need to differ between languages. JavaScript might not work as you expect it, at least in comparison with PHP.

1 Comment

I appreciate your answer. I'll use Bart Riemens advice.

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.