pheromix
pheromix

Reputation: 19307

How to set the value of a text HTML input field by using JQuery?

There is a HTML element of type text whose name is fiche_tache_ref. Inside a javascript file (.js) outside of the page's folder I want to set its value according to the value of a listbox selected :

function changerNatureRefTache(nature_tache_id) { // nature_tache_id is from a listbox

    var http_ajax = $('#http_ajax').val();
    var html = $.ajax({
    data: "nature_tache_id="+nature_tache_id ,
    type: "POST",
    url: http_ajax+"referentiel/AjaxTacheChangerNatureTache.php" ,  
    async: false
    }).responseText;

        // here I want to set the value of the field

}

Code of AjaxTacheChangerNatureTache.php is :

<?php
    if (isset($_POST['nature_tache_id'])) {
        define("ROOT_PATH", "../../");
        require_once ROOT_PATH  . 'config.inc.php';
        require_once RP_MODELS  . 'nature_tache.class.php';
        $db     = new DbConn();
        $obj    = new nature_tache($db->getInstance());
        $ret    = $obj->recupererNatureTache($_POST['nature_tache_id']);
        $code   = $ret['nat_code'];
        echo $code;
    }
?>

So how to set the value of the field using JQuery ?

Upvotes: 1

Views: 2076

Answers (4)

jerry
jerry

Reputation: 2789

you'll get lots of the same answer to this question, because this is kind of the low hanging fruit in the jquery docs.

$("selector").val("new value");

but one tip I'll give you that will make your php a little cleaner is instead of defining a root path like this:

define("ROOT_PATH", "../../");

you can set an include path in an .htaccess file using the following code:

php_value include_path ".:/path/to/your/application"

and then you can require files relative to that path (i.e. require_once('config.inc.php') as opposed to what your essentially doing, which is require_once('../../config.inc.php').

The .htaccess file should live in the main directory that holds your application.

Upvotes: 0

Priyank Patel
Priyank Patel

Reputation: 6996

Use the val() function of Jquery.

$('#IdOfTextbox').val("Some Text");

Further Reference

Upvotes: 1

Rasmus
Rasmus

Reputation: 8476

I dont get you correctly but something like this is used to set the value of input element in jquery:-

$("#elementid").val("myvalue");

Upvotes: 2

Jon Taylor
Jon Taylor

Reputation: 7905

Use the val() function

$('#idofinput').val("text you want in the field");

Since you are using name you can do the following

$('input[name=fiche_tache_ref]').val("text you want in the field");

Since names are not necessarily unique be careful with using the name as a selector. I would generally advise using an id instead.

Upvotes: 4

Related Questions