JO JOJO
JO JOJO

Reputation: 105

jQuery result in text field

How to put jquery result in text field. Here is the code: jquery:

$(document).ready(function () {
        $('p, li, a, href').click(function () {
            var xpath = getXPath(this);
            alert(xpath);
        });
    });

and HTML text field:

<input name="" type="text" value="">

What I need is instead alert put result in text field?

UPDATE:

Sorry for my bad question...

The result must be in selected text field:

<input id="" name="" type="text" value="">
<input id="" name="" type="text" value="">
<input id="" name="" type="text" value="">

and must write result in selected text field...

Upvotes: 0

Views: 418

Answers (6)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

var xpath = getXPath(this);
$("#yourInputId").val(xpath);

Upvotes: 1

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

you can use this to select the focused element

$(':focus').val(xpath);

this one is even much better that you give them all a name for example:

<input id="" name="myinput" type="text" value="">
<input id="" name="myinput" type="text" value="">
<input id="" name="myinput" type="text" value="">

then select them like this:

$('input[name="myinput"]').is(":focus").val(xpath);

if your textbox loose focus when you try to trigger script do this:

var selectedtextbox;
$('input[name="myinput"]').focus(function(){selectedtextbox=$(this);});

then in your sctipt use:

selectedtextbox.val(xpath);

make sure that selectedtextbox is global and can be accessed from your script.

Upvotes: 0

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

well simply you can give your input a id:

<input name="" type="text" id="myinput" value="">

and then set its value attribute:

$(#myinput").val(xpath);

Upvotes: 0

Ahmet Can G&#252;ven
Ahmet Can G&#252;ven

Reputation: 5462

if you have more than 1 input at your page you must give an 'id' to your input

 <input name="" type="text" id="myinput" value="">

Then you can select your input with $('#myinput')

This version below shows the situation of only one input in page. If there is more than 1 input in your page you must change you input like above and change $('input') to $('#myinput')

$(document).ready(function () {
        $('p, li, a, href').click(function () {
            var xpath = getXPath(this);
            $('input').val(xpath);
        });
    });

Upvotes: 1

Marek Sebera
Marek Sebera

Reputation: 40621

  1. Give that field an id attribute
    <input name="" type="text" value="" id="thatfield">
  2. Put the value to the field
    $("#thatfield").val(xpath)

code:

$(document).ready(function () {
    $('p, li, a, href').click(function () {
        var xpath = getXPath(this);
        $("#thatfield").val(xpath);
    });
});

documentation:

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

Upvotes: 1

danludwig
danludwig

Reputation: 47375

$(document).ready(function () {
        $('p, li, a, href').click(function () {
            var xpath = getXPath(this);
            $('#inputId').val(xpath)
        });
    });


<input id="inputId" name="" type="text" value="">

Upvotes: 1

Related Questions