# TargetConnected callback being called before access to element

**URL:** <https://discuss.hotwired.dev/t/targetconnected-callback-being-called-before-access-to-element/5895>\
**Category:** General\
**Created:** [July 18, 2024, 8:12pm UTC](https://discuss.hotwired.dev/t/targetconnected-callback-being-called-before-access-to-element/5895 "2024-07-18T20:12:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![awac](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@awac](https://discuss.hotwired.dev/u/awac)\
**Post date:** [July 18, 2024, 8:12pm UTC](https://discuss.hotwired.dev/t/targetconnected-callback-being-called-before-access-to-element/5895/1 "2024-07-18T20:12:20Z")

</div>

I’m using a Turbo stream to create a new row in a table, which works fine, but after creating, I need to set focus on the new row for accessibility considerations.

From the console.log, I can view the newly created row / element, but when trying to set focus on it below, I don’t have access to it (assuming from the doc, that the callback is triggered before actual connection).

If I wrap the row in question that was newly created in an old school jquery ready function, the focus set works.

I’m a bit perplexed. I would think the whole point of this callback is exactly for this purpose, to have access to a newly created element. What am I missing?

I saw a similar post regarding version, but I think my version here is newer than mentioned in the post where it was an issue.

```auto
"@hotwired/stimulus": "^3.1.1",

```

```auto
stimulus-rails (1.3.3)

```

```auto
  descriptionFieldTargetConnected(element) {
    console.log(element)
    element.tabIndex = 0
    element.focus()
    
    let rowId = document.getElementById('pcr_form_documents').lastElementChild.dataset.index
    $( document ).ready(function() {
      document.getElementById(`pcr_form_pcr_form_documents_attributes_${rowId}_description`).tabIndex = 0
      document.getElementById(`pcr_form_pcr_form_documents_attributes_${rowId}_description`).focus();
    })
  }

```
