1

As the title says, I'm trying to figure out how I can show a element based on whether or not a input field has focus our not.

So when the specific input field has focus I need to show a element, and when it has no focus the element should be hidden.

I've tried a lot of different things, and this is my lates try:

    <script>
     $(".only-oslo-delivery").hide();

     if ($("#address_city").is(":focus")) {
      $(".only-oslo-delivery").show();
     };
    </script>

only-oslo-delivery is a < p > tag with the text I want to display when the input field has focus.

address_city is the ID of the input field.

5 Answers 5

1

Your code only runs once, you need event handler to execute it as per user actions:

$("#address_city").on('focus', function() {
  $(".only-oslo-delivery").show();
}).on('blur', function() {
  $(".only-oslo-delivery").hide();
})
.only-oslo-delivery {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="address_city">
<p class="only-oslo-delivery">test</p>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that was exactly what I was looking for. I didn't know I needed blur as well. I'm very new to jquery/javascript.
0

You can bind events to your button to show/hide the .only-oslo-delivery element when it is focused and blurred.

$('#address_city').focus(function() {
  $('.only-oslo-delivery').show();
}).blur(function() {
  $('.only-oslo-delivery').hide();
});

Interestingly if the elements are next to each other on the page you could use adjacent css rules to accomplish this without any javascript.

<input type="text" id="address_city">
<p class="only-oslo-deliver">...</p>

And then use the following css:

.only-oslo-delivery {
   display: none;
}
#address_city:focus~.only-oslo-delivery {
   display: block;
}

Comments

0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="address_city">
<p class="only-oslo-delivery">test</p>

.only-oslo-delivery {
  display: none;
}

$("#address_city").on('focus', function() {
  $(".only-oslo-delivery").show();
}).on('blur', function() {
  $(".only-oslo-delivery").hide();
})

Comments

0

You need to use focus event to show and blur to hide:

$("#address_city").on('focus blur', function(e) {
  $(".only-oslo-delivery").toggle($('#address_city').is(':focus') && e.type === 'focus');
});
.only-oslo-delivery {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id='address_city'>
<p class='only-oslo-delivery'>only-oslo-delivery</p>

.toggle(true/false) would show when true and hide when false.

Comments

0

Here is another way:

var $field = $('#address_city');
var $info = $('.only-oslo-delivery').hide();

$field.on('focus blur', function (e) {
  $info.toggle(e.type === 'focus');
});

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.