Mark
Mark

Reputation: 637

How to select value in input field with jquery?

I'm trying to get the value in my input field to be put into my post statement to be sent to my php file to update the record without reloading the page. It works fine with static information that I put in but when I want to select what I have typed in the input box, i can't get it to work. Nothing happens.

<script src="../jquery.js"></script>
<script type="text/javascript">
    function editItem() {
        catno = $("#catno").attr('value');
        id = $("#id").attr('value');
        $.post('writeToDB.php', {
            id: id,
            catno: catno
        });
    }
</script>
</head>
<body>
    <form id="foo">
        <input type="hidden" value="<?php echo $row_Recordset1['ID']; ?>" name="id"
        id="id" />
        <input type="text" value="<?php echo $row_Recordset1['CAT_NO']; ?>" name="catno"
        id="catno" onchange="editItem();" />
    </form>

I'm new to this javasrcipt world and jquery but I'm at the piont of pulling my hair out. I'm probably doing something really stupid

Thanks

Upvotes: 1

Views: 7287

Answers (3)

Shyju
Shyju

Reputation: 218732

call val() method

id = $("#id").val();

val() method get the current value of the first element in the set of matched elements

so your code will be

function editItem() {
    var catno = $("#catno").val();
    var id = $("#id").val()
    $.post('writeToDB.php', {
        id: id,
        catno: catno
    });
}

Upvotes: 1

Manse
Manse

Reputation: 38147

Change

catno = $("#catno").attr('value');
id = $("#id").attr('value');

to

var catno = $("#catno").val();
var id = $("#id").val();

Use .val() to retrieve the value of an input.

You should also prefix your locally declared variables with var - this question/answer has a good explanation why

Upvotes: 9

Cl&#233;ment Andraud
Cl&#233;ment Andraud

Reputation: 9269

Use .val() :

http://api.jquery.com/val/

Upvotes: 0

Related Questions