1

When the user selects from a drop down box, I want to change the value of an input field to a jquery object, which is a span element from another page. This is working fine when I append to #myDiv; the span element shows up. However, I am trying to change a text input value, and this shows "Object object" on select change. I don't understand this inconsistency. How can I get the value to change correctly, as #myDiv does? Thank you.

<script>

function schedule(selectedValue){

var selectedValue = selectedValue - 1;

$.get('/file.xml', function(data)
{
    var $slides = $(data).find('span.desctext'),
    selectedslide = $slides.eq(selectedValue);

    $('#myDiv').append(selectedslide);
    $("#fvdescription").val(selectedslide);

});

}
</script>

HTML:

<form action="preview.php" method="post">
Action: <select name="whichslide" onchange="schedule(this.value)">
<option value="ab">Add & Bump</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option></select><br />
Year: <input type="text" name="year" size="3"> <br> URL: <input type="text" name="url" size="35"> <br> Description: <input type="text" name="description" id="fvdescription" size="75" value="Description goes here">  <br>
<input type="submit" name="preview" value="Preview">

2 Answers 2

1

Well, you can't put an element as the value of a textbox. It's basically just putting the toString value of your jQuery object as the value, which will always be "Object object" if it returns a result set.

What exactly are you wanting to display in your textbox? If you want to text from inside the span, do this:

$("#fvdescription").val(selectedslide.html());

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the reply, but this way isn't working for me either... nothing is changed in the input box. I added my HTML to original post.
0

Here selectedslide is a jQuery Object

$('#myDiv').append(selectedslide);

In this case you are appending the object to the div element

$("#fvdescription").val(selectedslide);

But here you are writing the value directly to a text field which shows as object in your textfield.

try this instead

$("#fvdescription").val(selectedslide.html());

4 Comments

thanks for the reply. noting is showing with the .html method; I try alert box with alert(selectedslide.html()), and yet nothing shows.
What is $slides here... Did you try logging it in console
$slides refers to six span elements on file.xml
i figured it out: var year = selectedyear.text(); $("#fvyear").val(year);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.