Rahul Iyer
Rahul Iyer

Reputation: 21025

Set the value of an input field?

I have this:

<body>
    <header></header>
    <h1>Request Group Rate</h1> 
    <form>
        <input type="hidden" id="referrer">
        //...
    </form>

    <script>
        $(document).ready(function(){
            $('input[id="referrer"]').val(document.referrer);
        });
    </script>

How so I set the value attribute of the <input> with id="referrer"?

Upvotes: 0

Views: 270

Answers (3)

Erik Philips
Erik Philips

Reputation: 54646

What sort of trouble are you having? because this works fine

        $(document).ready(function(){
            $('input[id="referrer"]').val(document.referrer);
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <input type="text" id="referrer" style="width:100%">
</form>

Althought I would use:

$('#referrer').val(document.referrer);

Upvotes: 0

user7110739
user7110739

Reputation:

$(document).ready(function() {
    $("input[id=referrer]").attr('value', document.referrer);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
    <h1>Request Group Rate</h1>
    <form>
        <input type="text" id="referrer" value="" style="width:100%">
    </form>
</body>

Upvotes: 0

Sahil Chhabra
Sahil Chhabra

Reputation: 11716

It looks fine and should work. You can try using # once.

$('#referrer').val(document.referrer);

Upvotes: 1

Related Questions