1

I am trying to understand bookmark application which I found on internet,here is the link for the code https://github.com/bradtraversy/bookmarker/blob/master/js/main.js Now in that code on line 85 he build button element with onclick event,assign it to function deleteBookmark() and passed the argument url in it which then get recieved in deleteBookmark() function on line 55 and his code works but when I tried to build similar kind of code to better understand what is happening in that application,my code does not work. I am using the code below.

    <div id="divArgument"></div>
    <input type="text" id="argument">
    <p id="displayArg"></p>
    <script>
    var testArg = document.getElementById('divArgument');
    var getVal = document.getElementById('argument').value;
    testArg.innerHTML = '<button onclick="sendArg(\''+getVal+'\')">Display Argument</button>';

    function sendArg(recVal){
            document.getElementById('displayArg').innerHTML = recVal;
        } 
    </script>

1 Answer 1

2

When '<button onclick="sendArg(\''+getVal+'\')">.... is executed getVal does not hold any value.

So put var getVal = document.getElementById('argument').value; inside the event handler function

var testArg = document.getElementById('divArgument');
testArg.innerHTML = '<button onclick="sendArg()">Display Argument</button>';

function sendArg(recVal) {
var getVal = document.getElementById('argument').value;
  document.getElementById('displayArg').innerHTML = getVal;
}

DEMO

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

1 Comment

how to append value for every time some thing is typed in input field using same code

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.