# Pressing "Add edit reason" button does not auto-focus into the text field

**URL:** https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140
**Category:** Bug
**Created:** [6 Febbraio 2016, 9:17am UTC](https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140 "2016-02-06T09:17:30Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![GaurangTandon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gaurangtandon/32/52384_2.png) [@GaurangTandon](https://meta.discourse.org/u/GaurangTandon)
#### Post date: [6 Febbraio 2016, 9:17am UTC](https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140/1 "2016-02-06T09:17:30Z")

</div>

This is just a really small bug and can easily be fixed.

**Summary:** When we press the “Add edit reason” button, the text field that comes after pressing that button does not get auto focus.

**Steps to reproduce:**

1. Open any of your posts and click the pencil icon for editing. You’ll see this at the top: ![](https://global.discourse-cdn.com/meta/original/3X/2/3/23eaf799e74df43275e6616767db74c74808cb4f.PNG)
2. Click the “add edit reason” button there. This appears: ![](https://global.discourse-cdn.com/meta/original/3X/1/4/14905b2dbe2cd4272bdd1ba2b605f3e330307772.PNG)
3. As you can see, the text field does not have focus and I have to manually click that field to get what I want: ![](https://global.discourse-cdn.com/meta/original/3X/4/6/46493da1a6f29e5f7578e01f12059da6c36b21ea.png)

**Expected results:** The text field should be **highlighted automatically** on step 2 above.

**Actual results:** The text field **does not get automatic highlight** on step 2 above.

**Version:** The one that’s running on this site, right now, I don’t know how to get it.

**System information:** Any system will see this bug hopefully, but I am using Windows 7 with Google Chrome 48.

Hope that’s a quick fix 😃

---

<div class="post-metadata">

### Author: ![GaurangTandon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gaurangtandon/32/52384_2.png) [@GaurangTandon](https://meta.discourse.org/u/GaurangTandon)
#### Post date: [23 Marzo 2016, 9:46am UTC](https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140/2 "2016-03-23T09:46:42Z")

</div>

Solution found. In this file:

> <https://github.com/discourse/discourse/blob/eb5cee3150fbd0b2de08d2a93b3959a20e41a691/app/assets/javascripts/discourse/controllers/composer.js.es6>

Line 173+ need to be something like this:

```
displayEditReason() {
  this.set("showEditReason", true);
  # a way to get input#edit-reason and call .focus() on it
},

```

I don’t know how to interact with DOM in this file, is it simply `document.querySelector()`? If someone can tell me this, I will submit a PR with fixes 🙂

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [23 Marzo 2016, 9:55am UTC](https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140/3 "2016-03-23T09:55:01Z")

</div>

I agree this would be a good change there’s also an alignment issue there @zogstrip, can we assist @GaurangTandon in figuring this out?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [23 Marzo 2016, 4:04pm UTC](https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140/4 "2016-03-23T16:04:12Z")

</div>

> [@GaurangTandon](#):
>
> I don’t know how to interact with DOM in this file

That’s because you don’t 😉 DOM interactions should only be done in a view.

You will need to catch the click in the view, bubble the event up to the controller and then do your DOM `blur`.

---

<div class="post-metadata">

### Author: ![GaurangTandon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gaurangtandon/32/52384_2.png) [@GaurangTandon](https://meta.discourse.org/u/GaurangTandon)
#### Post date: [24 Marzo 2016, 2:55am UTC](https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140/5 "2016-03-24T02:55:05Z")

</div>

> [@zogstrip](#):
>
> That’s because you don’t 😉 DOM interactions should only be done in a view.

Oh, is Discourse using the Model View Controller kind of practice? I have only heard about it, but never studied, practiced or used it. Seems like a good time to learn some MVC 🙂 I will try to complete the first step you said.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [24 Marzo 2016, 3:21am UTC](https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140/6 "2016-03-24T03:21:54Z")

</div>

> [@GaurangTandon](#):
>
> Oh, is Discourse using the Model View Controller kind of practice?

“Yes” would be an extreme understatement.

My simplified answer would be  
Discourse has a Rails backend (and yes, Rails is MVC)  
with an Ember frontend (and yes, Ember is MVC)

Most of what I have exploring is es6 (a flavor of JavaScript) and some hbs (handlebars/htmlbars templates) files so I would recommend the [Ember documentation](https://guides.emberjs.com/v2.4.0/).  
There is a lot there, almost overwhelmingly so (for me anyway), but it will definitely help you get your toes wet.

---

<div class="post-metadata">

### Author: ![GaurangTandon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gaurangtandon/32/52384_2.png) [@GaurangTandon](https://meta.discourse.org/u/GaurangTandon)
#### Post date: [24 Marzo 2016, 3:30am UTC](https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140/7 "2016-03-24T03:30:00Z")

</div>

Thanks @Mittineague 🙂 I have been looking into the files, and from what I can see, this line in the handlebars file:

```
<a {{action "displayEditReason"}} class="display-edit-reason">{{i18n 'composer.show_edit_reason'}}</a>

```

calls the `displayEditReason` click handler here:

```
displayEditReason() {
  this.set("showEditReason", true);
}

```

and then, since `showEditReason` boolean becomes true, the `if` branch gets executed in this:

```
{{#if showEditReason}}
    <div class="edit-reason-input">
          {{text-field value=editReason tabindex="7" id="edit-reason" maxlength="255" placeholderKey="composer.edit_reason_placeholder"}}
    </div>
{{else}}
    <a {{action "displayEditReason"}} class="display-edit-reason">{{i18n 'composer.show_edit_reason'}}</a
{{/if}}

```

and we see the `.edit-reason-input` field. I then saw this line in the [view](https://github.com/discourse/discourse/blob/eb5cee3150fbd0b2de08d2a93b3959a20e41a691/app/assets/javascripts/discourse/views/composer.js.es6):

```
@observes('composeState', 'composer.action')

```

which makes me feel that something like this:

```plaintext
@observes('showEditReason', function(){
    var input = $(".edit-reason-input input");
    if(input) input.focus();
})

```

might be possible.

Or maybe I again mixed up the view code and the controller code so I will first read the docs 😆

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [27 Giugno 2016, 6:50am UTC](https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140/8 "2016-06-27T06:50:27Z")

</div>

Not really a bug… but fixed none the less 🙂

[https://github.com/discourse/discourse/commit/3ad1423c5390595cdf80242815d6e43412a12ae7](https://github.com/discourse/discourse/commit/3ad1423c5390595cdf80242815d6e43412a12ae7)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [27 Giugno 2016, 6:50am UTC](https://meta.discourse.org/t/pressing-add-edit-reason-button-does-not-auto-focus-into-the-text-field/39140/9 "2016-06-27T06:50:30Z")

</div>


