Use 'this' keyword as a selector

I would use the selector of my event to put in my function, but it doesn't work.

I already try some tricks I'll show in the following code.

// the actual code
$('#voyages').on('click', function() {
  if (continentOuvert == false) {
    $('#voyages i.fas.fa-arrow-up').show(500);
    $('#voyages i.fas.fa-arrow-down').hide();
  }
});

// the functions I try to make
function arrowUp() {
  $('this i.fas.fa-arrow-up').show(500);
  $('this i.fas.fa-arrow-down').hide();
}

// or
function arrowUp() {
  $(this + ' i.fas.fa-arrow-up').show(500);
  $(this + ' i.fas.fa-arrow-down').hide();
}

Nothing happens, I think I didn't do right.

Thanks for your help.

You can use the find method:

$(this).find('i.fas.fa-arrow-up').show(500);
$(this).find('i.fas.fa-arrow-down').hide();

Use, describing an action or state of affairs that was done repeatedly or existed for a period in the past. use, employ, utilize mean to put into service especially to attain an end. use implies availing oneself of something as a means or instrument to an end. willing to use any means to achieve her ends employ suggests the use of a person or thing that is available but idle, inactive, or disengaged. looking for better ways to employ their skills utilize may suggest the discovery of a new, profitable, or practical use for something. an old wooden bucket utilized as a planter

As already mentioned $(this) cannot be a selector because it's an object not a string. As an alternative you can use $(e.target) which points to the clicked element (ie .voyages), always pass the Event Object (it's the e variable in the demo). .toggleClass() will streamline your code. Also jQuery is flexible so do not use #id use .class as much as possible.

$('.voyages').on('click', function(e) {
  var icon = $(e.target).find('.fas');
  icon.toggleClass('fa-flip-vertical');
});
button {
  cursor: pointer;
  font-size: 24px
}
<link href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" rel="stylesheet" crossorigin="anonymous">

<button class='voyages'>Voyages <i class='fas fa-arrow-up'></i></button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Use Synonyms, Use Antonyms, be or become familiar with someone or something through experience. Use definition, to employ for some purpose; put into service; make use of: to use a knife. See more.

Thanks you for all your reply. I finally use this code :

function arrowUp(id) {
            $(id + ' i.fas.fa-arrow-up').show(500);
            $(id + ' i.fas.fa-arrow-down').hide();
        }

Use, the action of using something or the state of being used for some purpose. 1. employ, utilize, make use of, work, apply, operate, exercise, practise, resort to, exert, wield, ply, put to use, bring into play, find a use for, avail yourself of, turn to account Officials used loud hailers to call for calm.

use, verb (used with object), used, us�ing. to employ for some purpose; put into service; make use of: to use a knife. to avail oneself of; apply to one's own purposes: to use the facilities. to expend or consume in use: We have used the money provided. to put something such as a tool, skill, or building to a particular purpose: This glass has been used - could you get me a clean one? [ + to infinitive ] Use scissors to cut the shapes out. Going on the expedition gives me a chance to use all the training I've had.

USE (verb) definition and synonyms, Another word for use. Find more ways to say use, along with related words, antonyms and example phrases at Thesaurus.com, the world's most trusted free� The words employ and utilize are common synonyms of use. While all three words mean "to put into service especially to attain an end," use implies availing oneself of something as a means or instrument to an end. willing to use any means to achieve her ends Where would employ be a reasonable alternative to use?

USE, 1.2count noun A purpose for or way in which something can be used. More example sentences. 'the herb has various culinary uses'. Synonyms. We use cookies and other digital information to make site navigation easier, analyze site usage, and for marketing purposes.

Comments
  • You should not use this + string/class names in your arrowUp function, as this is an object and doing so will not give you any reference but only a very vague string like [object]strstrstr
  • A tip, do console.log( $(this).find('i.fas.fa-arrow-up') ) in your code and checl in browser console what element you get by that. this will help you to understand what the code is doing / selecting
  • Alternative syntax: $("i.fas.fa-arrow-up", this).show(500);