0

I'm trying to append the following javascript to a div that will appear when the user presses the ESC key. I can't seem to get the JavaScript to display the value of the javascript being used:

    $("#json").hide();
    $(document).keydown(function (e) {
    if (e.keyCode == 27) {
         $("#json").append("<script type="text/javascript"> + "window.location.href = '/editorialPortal' + '?' + this.id + '=' + this.options[this.selectedIndex].value;" + "</" + script>").toggle();
    }
});

I'm not sure where I'm missing string escaping or a quotation mark.

4
  • What? You want to go to a new page when the user presses escape? Commented Dec 17, 2014 at 20:41
  • No, that JavaScript pulls the current URI plus another parameter from a dropdown, which is used to provide results from the dropdown selection. This javascript should show the current URI based on that selection, which is being hidden from the basic user. Commented Dec 17, 2014 at 20:43
  • Well this doesn't refer to a dropdown, it refers to the document - and window.location.href is used to change the page location, and toggle is used to hide and show an element... there's a lot wrong. What is your end goal? Commented Dec 17, 2014 at 20:44
  • Ignore the dropdown selection part(I was explaining the use of that URI). The idea is to simply grab the current URI and append it to the div that is being triggered on keypress. Commented Dec 17, 2014 at 20:46

2 Answers 2

1

Try e.which rather than e.keyCode

Also, since you opened the string with double quotes, you cannot use double quotes in the string (open it with single quotes, and only use single-quotes when you want to use ' + ' or close the whole string).

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

Comments

0

I think the problem is with the quotes:

$('#json').append('<' + script type="text/javascript">window.location.href = "/editorialPortal?' + this.id + '=' + this.options[this.selectedIndex].value + '"  </' + 'script>').toggle();

In this example, I use single quotes in defining strings, so the double quotes can be used inside it.

Also, be sure to include jQuery in your fiddles. The above code works except that it errors on the undefined variables this.options[this.selectedIndex].value and this.id. Breaking up of the script tags is to make jsfiddle happy.

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.