1
var send_value = [current_site_id, current_site_name, current_site_description];
console.log(send_value);
send_value = jQuery(this).is(':checked') ? send_value.push('add') : send_value.push('delete');
console.log(send_value);

The result is

["12", "qwrqweqwer", "qwreqwr"]
4

Where i'm wrong?

PS. Sorry, my mistake was in send_value =, can not understand how it got there

3 Answers 3

3

This line send_value = jQuery(this).is(':checked') ? send_value.push('add') : send_value.push('delete'); assigns the return value of push to send_value, it doesn't push anything. The return value is the new length of the array object, as you can see in the specifications

Try this:

send_value.push(jQuery(this).is(':checked') ? 'add' : 'delete');

Push the resulting value of your ternary
If you find it more readable, you could turn your ternary into an expression, rather then a statement:

(jQuery(this).is(':checked') ? send_value.push('add') : send_value.push('delete'));//<-- notice surrounding parentheses

But, frankly, if your concerned about readability, it's best to do away with ternaries all together IMHO.

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

3 Comments

The items are pushed in the array but he has lost a reference to the array
@Esailija: I left that out, for the OP to discover :P, that's why I linked to the specifications, and hinted at the return value being the new length of the array object
my mistake was in send_value =... I don't know for what i use it, but thanks.
3

push returns the length of the array after the pushes are done. Try this:

var send_value = [current_site_id, current_site_name, current_site_description];
console.log(send_value);
send_value.push( jQuery(this).is(':checked') ? 'add' : 'delete' );
console.log(send_value);

Additionally you should use an object not an array. send_value.site_id reads better than send_value[0]...

1 Comment

It should be mentioned that ternary expressions with side effects are considered bad practice :)
1
var result = jQuery(this).is(':checked') ? 'add' : 'delete';
    send_value.push(result);

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.