0

I wrote this little bit of code but I'm not sure why it's not working? It's supposed to take in the persons name and depending on what they selected it will output a website with their name at the end of it.

JSFiddle http://jsfiddle.net/tQyvp/135/

JavaScript

function generateDynamicSignature() {
    var dynSig = "";
    var user = document.getElementById("usernameInput");
    var e = document.getElementById("scriptListInput");
    var strUser = e.options[e.selectedIndex].text;
    if (strUser == "example") {
        dynSig = "http://example.com/users/";
    }
    document.getElementById("generateSignature").addEventListener('click', function () {
        var text = document.getElementById('dynamicSignatureOutput');
        text.text = (dynSig + user);
    });
}

HTML

<select class="form-control" id="scriptListInput">
                    <option value="example">Example 1</option>
                 </select>

0

5 Answers 5

1

There are a few problems with your code, I'll try to list them all.

First, you never added the username input to your HTML.

Next, you seem mixed up on the way to access/set the text of an HTML input. You do this through the value field. For the username input, you forgot to access any property, so you'll need to change it to:

var user = document.getElementById("usernameInput").value;

You later used the text property of both the select element and the output. These should also both be value.

Another problem is that you've placed a listener inside a listener. Your outer function, generateDynamicSignature, listens for the onclick event of the button. This function only runs after the button is clicked. But inside this function, you attach a new listener. This inner listener will only run if someone clicks the button twice.

I've included these changes in a new fiddle:

https://jsfiddle.net/zdfnk77u/

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

Comments

0
  • where is usernameInput in your html?
  • in the if, use === instead of ==

Comments

0

If and when you add the missing "usernameInput" element in your HTML, all you'll have to do is...

dynSig='http://example.com/users/'+usernameInput.value;

Comments

0

I think part of the problem is that you want to access the value and not the text of input elements. So for text and strUser, you want to do text.value instead of text.text and such.

Also, based on the JSfiddle, you probably want to rewrite how you're using the document listener and the onclick of the html element. Every time the button is clicked it goes through the generateDynamicSignature and creates a listener to change the value, but doesn't necessarily change the value itself. If you move the logic of the generate function inside the click listener, that should fix most of your problems.

Comments

0

You create your generateDynamicSignature inside $(document).ready. There are two approaches.

  • define function generateDynamicSignature outside $(document).ready

or

  • bind your button.click to a handler inside $(document).ready

Do not mix these two.

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.