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);

    });

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.
});

@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);

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.