N
N
newobj2015-10-27 15:30:07
JavaScript
newobj, 2015-10-27 15:30:07

How to return value from .click()?

Good afternoon.
Need your advice:
Task
There is a form with open and hidden fields.. they are connected. Example:

(Открытое)
<span class="twitter-typeahead" style="position: relative; display: inline-block;">
<input class="typeahead  form-control tt-hint" value="" type="text" readonly="" autocomplete="off" spellcheck="false" tabindex="-1" dir="ltr" style="position: absolute; top: 0px; left: 0px; border-color: transparent; box-shadow: none; opacity: 1; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">
<input class="typeahead  form-control tt-input" name="year_autocomplete" value="" type="text" id="year_autocomplete" autocomplete="off" spellcheck="false" dir="auto" style="position: relative; vertical-align: top; background-color: transparent;">
<pre aria-hidden="true" style="position: absolute; visibility: hidden; white-space: pre; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; word-spacing: 0px; letter-spacing: 0px; text-indent: 0px; text-rendering: auto; text-transform: none;">2</pre>
<div class="tt-menu" style="position: absolute; top: 100%; left: 0px; z-index: 100; display: none;"><div class="tt-dataset tt-dataset-0"><p class="tt-suggestion tt-selectable">2015</p><p class="tt-suggestion tt-selectable">*</p></div></div></span>

(Hidden)
<input name="year" value="1" type="hidden" id="year">

I need to check the value of a hidden field, after populating its associated public field.
How I did it:
find the id of the field that was clicked on (Field_name_complete), cut off the _complete part = get the id of the hidden field. Save. Return.
$(function(){
    var hiddenId = $("input.typeahead").click(function(){
       var idInputField = $(this).attr("id");//***_complete
       var result = idInputField.split("_").slice(0,1);//****
        //console.log(result);
        return result;
    });
    console.log(hiddenId);
    //if(!$("#"+hiddenId).value){
    //
    //}
});

The problem
does not return the result.hiddenId - empty
I would be grateful for your help. Since I can not solve the problem myself - I do not have enough experience with js.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
holfza, 2015-10-27
@newobj

Попробуйте так:
$(function(){
    var hiddenId;
    $("input.typeahead").click(function(){
        var idInputField = $(this).attr("id");//***_complete
        hiddenId = idInputField.split("_").slice(0,1);//****
    
    console.log(hiddenId);
    });
    
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question