0

I want to get input value but it inside li tag;

$(".enter_click").click(function() {
  var enter_click = $(this).children(".enter_click_id").val();
  console.log(enter_click);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li class="enter_click">
  Click me
  <input type="hidden" class="enter_click_id" value="hidden value">
</li>

Original HTML for input:

<input type="hidden" class="enter_click_id" value="' + value.id + '"> 

When the li is clicked I want to get the value of the input inside it. Even if I bind the click function directly to the input I cannot click because the input has no size.

When I write the li tags directly as html, the function works, but my li tags are added instantly with the javascript append function. I have a search bar. The data containing the characters written to the search bar are pulled from the json file and added to the ul tag with #result id. It doesn't work when you do it like this

<input type="input" name="search" id="search" placeholder="Type something" class="form-control" onfocusout="focus_out()"/>
<div style="position:relative;width:470px;">
<ul style="text-align:left;" class="list-group" id="result">

</ul>
</div>
<script>
$.each(data, function(key, value){

    if (value.name.search(expression) != -1 || value.location.search(expression) != -1)
    {
        
        $('#result').append('<li class="enter_click list-group-item link-class"><input type="hidden" class="enter_click_id" value="'+value.id+'"><img src="'+value.image+'" height="120" width="120" class="img-thumbnail" /> '+value.name+' | <span class="text-muted">'+value.location+'</span></li>');
    }
    

});   
  • Your code should work fine as it is - although you're missing a closing `)`. Check the console in devtools for errors. – Rory McCrossan Jan 18 '23 at 08:37
  • I've converted your code to snippet, adding jquery and the missing `)` along with a value. This shows the click event is working fine. Perhaps there is another issue such as not including jquery or the way you build the input in the first place `value="' + value.id + '"` is not HTML - so looks like it's built somehow - perhaps `value.id` doesn't have a value? – freedomn-m Jan 18 '23 at 09:21
  • From the edit-answer (please [edit] your question, don't add an answer that's not an answer) - it looks like your HTML is generated dynamically - for that you need event delegation. – freedomn-m Jan 18 '23 at 09:23
  • I shared a certain part of the code – Bora Özçağlar Jan 18 '23 at 09:33

0 Answers0