Javascript – HTML join input textfied value to href when link is clicked

htmljavascriptPHP

I've an input text field and a link, when pressing the link i want to get a url containing value of the input field like this:

if i write roma on the text field i want to go to the page treatment/exportPDF/roma.

What i tried:

<script>
    function getUsername(){
        return $(".username").Text();
    }
</script>
<div  class="container">
    <ul>
        <li>
            <label class="Paragraph">Select a username</label>
            <p>
                <label>Username</label>
                <input name="username" type="text" class="longInput1"/>
                <span class="errorMessage"></span>
            </p>
            <p>
                <a class="smallLink" href="<?php echo URL2; ?>Treatment/exportPDF/"> PDF</a>
            </p>
            <p>
                <a class="smallLink" href="">Excel</a>
            </p>
        </li>
    </ul>
</div>

so how could i call the function getUsername() from my href? is my script also wrong?

Best Answer

Try

<script>
    function getURL(){
        document.location = "<?php echo URL2; ?>Treatment/exportPDF/"+ $(".username").Text();
    }
</script>
<div  class="container">
    <ul>
        <li>
            <label class="Paragraph">Select a username</label>
            <p>
                <label>Username</label>
                <input name="username" type="text" class="longInput1"/>
                <span class="errorMessage"></span>
            </p>
            <p>
                <a class="smallLink" href="javascript:getURL()"> PDF</a>
            </p>
            <p>
                <a class="smallLink" href="">Excel</a>
            </p>
        </li>
    </ul>
</div>