0

I am trying to display value.
The code below displays value but it uses alert to show value.
But I need to show value in a textbox. Anyone there to help me???

$('#show').click(function() {
var $cars = '';
$('#selected_cars .innertxt2').each(function() {
var $car_value = $(this).attr('carvalue');
if ($cars == '')
$cars += $car_valu;
else
$cars += ',' + $car_valu;

        });

        alert($cars);

    });

Edited by layman114: code not displaying properly

2
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by Airshow
0

To display the result in the DOM, first hard-code a container in your HTML, eg. <div with id="message"></div>.

You will find it simpler to accumulate the carvalue strings in an array then using .join(glue) to put the pieces together before poking the resulting string into the DOM.

$('#show').click(function() {
    var $cars = [];//empty Array
    $('#selected_cars .innertxt2').each(function() {
        $cars.push($(this).attr('carvalue'));//add snippet to array
    });
    $("#message")($cars.join(','));//join the snippets with some glue.
    //$("#message")($cars.join('<br/>'));//alternative glue.
});
0

@Airshow Thanks for you reply
I manage to display value in a div. But I need in a input box
I only changed alert($users); to $("#result").html($users);

0

OK, for an input field, it's very slightly differnt:

$('#show').click(function() {
    var $cars = [];//empty Array
    $('#selected_cars .innertxt2').each(function() {
        $cars.push($(this).attr('carvalue'));//add snippet to array
    });
    //$("#result").text($cars.join(','));//for a <div> or <p> or <span>.
    $("#result").val($cars.join(','));//for an <input> or <textarea>.
});

(Commented out line is a corrected version of what I posted above).

Airshow

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.