0

how can I access the value of an input text field, which loads propably after the dom?

I am working with leaflet at the moment and a plugin creats an input field and I don't know, how to access it.

If I try this:

$('#myTextbox1').on('input', function() {
    alert($('#myTextbox1').val());
});

I don't get any result. I guess, jQuery can't handle the created input field.

My HTML looks like this:

@extends('layouts.app')

@section('head')
<link rel="stylesheet" type="text/css" href="{{ asset('css/leaflet/leaflet.css') }}">
<link rel="stylesheet" type="text/css" href="{{ asset('css/leaflet/leaflet-search.css') }}">
@ensection

@section('content')

<div class="col-md-8 col-md-offset-2 form-container">

<div id="map-adress"></div>

<div class="panel panel-default marker-panel">
    <div class="panel-heading">Create a post</div>
    <div class="panel-body">  
        <div class="col-md-8 col-md-offset-4">
            <p>Insert the adress of the marker position<br>
                (You can move the marker on the map to the right position).</p>
        </div>

        <div class="form-group">
            <label for="findbox-adress" class="col-md-4 control-label find-label">Marker Location</label>
            <div class="col-md-6">
                <div id="findbox-adress"></div>
            </div>

            <div class="col-md-2">
                <button type="submit" class="btn btn-primary user_marker_btn">
                    Validate
                </button>
            </div>
        </div>
    </div>
</div>

<div id="user_marker_adress" class="panel panel-default">
    <div class="panel-heading">Validate the marker adress</div>
    <div class="panel-body">

        <form class="form-horizontal" method="POST" action="{{ route('userposts.create') }}">
            {{ csrf_field() }}

            <div class="form-group">
                <label for="a_street" class="col-md-4 control-label">Street</label>
                <div class="col-md-6">
                    <input id="a_street" type="text" class="form-control" name="a_street" required>
                </div>
            </div>

            <div class="form-group">
                <label for="a_street_no" class="col-md-4 control-label">Street No.</label>
                <div class="col-md-6">
                    <input id="a_street_no" type="text" class="form-control" name="a_street_no" required>
                </div>
            </div>

            <div class="form-group">
                <label for="a_zip_code" class="col-md-4 control-label">Zip Code</label>
                <div class="col-md-6">
                    <input id="a_zip_code" type="text" class="form-control" name="a_zip_code" required>
                </div>
            </div>

            <div class="form-group">
                <label for="a_state" class="col-md-4 control-label">State</label>
                <div class="col-md-6">
                    <input id="a_state" type="text" class="form-control" name="a_state" required>
                </div>
            </div>

            <div class="form-group">
                <label for="a_country" class="col-md-4 control-label">Country</label>
                <div class="col-md-6">
                    <input id="a_country" type="text" class="form-control" name="a_country" required>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-8 col-md-offset-4">
                    <button type="reset" class="btn btn-danger">
                        Clear
                    </button>
                    <button type="submit" class="btn btn-primary">
                        Next
                    </button>
                </div>
            </div>

        </form>

    </div>
</div>

@endsection

@section('scripts')
<script type="text/javascript" src="{{ asset('js/leafleat/leaflet.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/leafleat/leaflet-search.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/leafleat/marker-adress.js') }}"></script>
@endsection

The div with the ID "findbox-adress" triggers the plugin, which will create the form with the input, that I can't access.

Hope, someone got an idear of my problem...

Edit: OK, I have found an ID in this element and with the ID it is working, but only the text, that I have typed in. The plugin gives me a dropdown selection(autocomplete like ajax) where I can choose a unordered list with some li's. But there I can't get a value? How can I get the value, if someone is clicking on the autocomplete(li's)?

My try:

$(".user_marker_btn").click(function(){
    console.log($("#searchtext9").val());
});
1
  • Use event delegation
    – JJJ
    Commented Jul 27, 2017 at 1:34

2 Answers 2

1

On dynamically created elements, use event delegation to attach event listeners.

Attach the event listener to a parent element that existed on DOM-load, and then pass the element that you wish to delegate the event to:

$('body').on('input', '#myTextbox1', function() { 
    alert($('#myTextbox1').val()); 
});
0
0

First of all, your given code doesn't look right. Please refer to jQuery manual: http://api.jquery.com/on/ This might be:

$( "body" ).on( "click", "input#myTextbox1" function() {
  console.log( $( this ).val() );
});

As you said, "The div with the ID "findbox-adress" triggers the plugin, which will create the form with the input" If you can track the event when the div 'findbox-adress' get clicked or triggered, you can easily check for the form whether it has been added in the DOM or not and then check for input field by using $("input#myTextbox1").val();

Or if you cannot track the event of loading the form, you will need to have a function to check always when the form is getting loaded into the DOM. That might be using setTimeout() or any other method by framework/library. And finally stop the tracking when the form loaded.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.