0

I am using the push method to add new values to an existing array (st[]). Adding a new value works, but then all values in the array get the value of the last added element.

if(!window.WordCatcher){
    WordCatcher = {};
}

WordCatcher.selector = {};

WordCatcher.selector.getSelected = function(){
    var t = '';
        if(window.getSelection) {t = window.getSelection();}
        else if(document.getSelection) {t = document.getSelection();}
        else if(document.selection) {t = document.selection.createRange().text;}
    return t;
}


st = new Array();

WordCatcher.selector.dblclick = function() {
    st.push(WordCatcher.selector.getSelected());
    console.log(st);
}

Call the function in jQuery with:

$(document).bind("dblclick", WordCatcher.selector.dblclick);

Example: If I double click first "Die", second "Smart", third "TV", I will get the following log in firebug:

[Die { constructor=Selection,  focusNode=textNode,  anchorNode=textNode,  mehr...}] [Smart {constructor=Selection,  focusNode=textNode,  anchorNode=textNode,  mehr...}, Smart {constructor=Selection,  focusNode=textNode,  anchorNode=textNode,  mehr...}] [TV { constructor=Selection,  focusNode=textNode,  anchorNode=textNode,  mehr...}, TV {constructor=Selection,  focusNode=textNode,  anchorNode=textNode,  mehr...}, TV {constructor=Selection,  focusNode=textNode,  anchorNode=textNode,  mehr...}]

Maybe somebody has an idea what I am doing work.

Best regards, Andy

2 Answers 2

1

I think your pushing a reference to "t".

everytime its changed - all your array elements are changing , because they are all referencing to the same parameter => "t".

the problem is probably in your function: WordCatcher.selector.getSelected

try to change it to return something else , and check again.

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

4 Comments

I just added console.log(t) to the getSelected() and could see that the return value for t seems to be right: Die { constructor=Selection, focusNode=textNode, anchorNode=textNode, mehr...} [Die { constructor=Selection, focusNode=textNode, anchorNode=textNode, mehr...}] Smart { constructor=Selection, focusNode=textNode, anchorNode=textNode, mehr...} [Smart { constructor=Selection, focusNode=textNode, anchorNode=textNode, mehr...}, Smart { constructor=Selection, focusNode=textNode, anchorNode=textNode, mehr...}]
i have no idea what "getSelection" is returning. and the console.log is messy.
change "getSelection" to a function that returns a simple string. or try changint return t; to something like return t.text();
Great. Now it works. I added t = t.toString(); in if(window.getSelection) {... Thanks for the help, IdanHen
0

Internal window's selection object is a sort of singleton, that is every getSelection() call returns the same reference to the same object. Thus, you have to manually grab all the data you need, or clone the object, but do not store the immediate 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.