c00L c00L - 1 year ago 81
Javascript Question

Access the value of an input that is loaded after the dom

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

Answer Source

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()); 
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download