0

Here's my code:

<script LANGUAGE="Javascript">
        function choiceReturn(obj) {
            switch(obj.value) {
                case "1":
                    document.write("<input value='Fund (L###)' type='text' 
                    class='button3' onfocus='if(this.value == "Fund (L###)"){this.value = '';}' 
                    onblur="if(this.value == ''){this.value = 'Fund (L###)'}">");

            }
        }
        </script>

<select name="funds_menu" onchange="choiceReturn(this);">
            <option selected>Choose 1 to 3</option>
            <option value="1">1. Choose fund</option>
            <option value="2">2. Prepare data</option>
            <option value="3">3. Perform valuation</option>
            </select>

My intention: Upon selecting the first item from the list, the Fund (L###) input box appears. I also have some minor code onblur and onfocus to make this a bit cooler.

However, nothing happens. What am I doing wrong? There's a lot of quotation marks there. Perhaps that's what's messing this up.

4
  • document.write deletes everything else on the page. Commented Jul 7, 2014 at 17:29
  • Oh, I see. Do you know how to input HTMl inside Javascript code? Commented Jul 7, 2014 at 17:29
  • 2
    Learn how to debug JavaScript Commented Jul 7, 2014 at 17:30
  • @gud ideally, you should be using dom manipulation. If you really want to use html, there is `.innerHTML Commented Jul 7, 2014 at 17:45

1 Answer 1

2

Multiline strings aren't a thing in JavaScript. You're going to have to concatenate them separately. Furthermore, some of your nested nested strings aren't escaped properly.

Fixed code:

document.write("<input value='Fund (L###)' type='text' " +
    "class='button3' onfocus='if(this.value == \"Fund (L###)\"){this.value = \"\";}' " + 
    "onblur='if(this.value == \"\"){this.value = \"Fund (L###)\"}'>");

However, document.write is generally A Bad Idea™, and DOM manipulation via methods like document.createElement, element.setAttribute (or setting them directly with the JS DOM API), element.addEventListener, and element.appendChild are almost always a better approach.

Here's the code rewritten to not use document.write:

var inputElement = document.createElement('input');
inputElement.value = 'Fund (L###)';
inputElement.type = 'text';
inputElement.className = 'button3';
inputElement.addEventListener('focus', function() {
    if (this.value == "Fund (L###)") {
        this.value = "";
    }
});
inputElement.addEventListener('blur', function() {
    if (this.value == "") {
        this.value = "Fund (L###)"
    }
});
document.getElementById('someElement').appendChild(inputElement);

Also note that what you're trying to do already exists: placeholder="Fund (L###)".

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

4 Comments

That is indeed a bad idea. All of the webpage is erased when I use write() as Jeff Shaver said! I tried all of these but none of them work so I'm doing something wrong. Can't I just replace them with write()?
@Gudmundur I've edited the answer to include further information.
You can have multiline strings without having to append strings to strings, you just need to put a `\` at the end of each line inside the string. It's generally considered to be bad coding practice, though, not to mention it's sort of inconvenient.
Thank you for your help Doorknob!

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.