0

I wrote a function that renders all my selected fields (seats) in a div class.. When I click on them they need to be shown by their name.. Now, I want that fields to be written in the input field I selected on click..

var rowRange = 'abcdefghijklmnopqrstuvwxyz'.split('');

var $cart = $('#selected-seats'),
    sc = $('#seat-map').seatCharts({
    map: [
        'aaaaa__aaaaa',
        'aaaaa__aaaaa',
        'aaaaa__aaaaa'
    ],

    naming : {
        top : false,
        getLabel : function (character, row, column) {
            return rowRange[row - 1].toUpperCase() + column;
        },
    },

    click: function () {
        if (this.status() === 'available') {
            $('<li>'+this.settings.label+'</b></li>')
                .appendTo($cart);


            $counter.text(sc.find('selected').length+1);

            return 'selected';
    });
});

and this is my form input field

 <input type="text" id="selected-seats" class="form-control"  size="20">

This example works fine but I have trouble connecting it with input field..

if (this.status() == 'available') {
            $('<li>'+this.settings.label+'</b></li>')
                .appendTo($cart);

This does not..

if (this.status() == 'available') {
            document.getElementById('selected-seats')
                .appendTo($cart);
2
  • What about $('#selected-seats').appendTo($cart)? Commented Nov 15, 2018 at 11:45
  • Also tried. Still nothing. Commented Nov 15, 2018 at 11:49

2 Answers 2

1
document.getElementById('selected-seats')

The line above returns a pure DOM element, not wrapped by any jQuery object. DOM elements don't have the appendTo method - jQuery objects do however. So what you need to do is either to use jQuery to get the element by ID (first example below) or use it to wrap the result of the native getElementById method (second example below).

// First example
$('#selected-seats').appendTo($cart)

// Second example
var selectedSeatsElement = document.getElementById('selected-seats');
$(selectedSeatsElement).appendTo($cart)
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your fast response. I tried both ways and it doesn't work. I am starting to think it can be related to my input field?
It might be. Does it find the element when you invoke the getElementById method yourself?
It's not triggering! :/
Could you be more specific?
Everything is passing without any errors. This part does work $('<li>'+this.settings.label+'</b></li>'), but when it comes on the example with input field it does nothing..
|
0

I just fixed it!

I needed to return value for input field.

$('#selected-seats')
                 .attr('id', 'cart-item-'+this.settings.id)
                 .val(this.settings.label)
                 .appendTo($cart);

Comments

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.