Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextBox can not get value, if value is filled by javascript #3159

Closed
1 task done
butaixianran opened this issue Feb 9, 2023 · 2 comments
Closed
1 task done

TextBox can not get value, if value is filled by javascript #3159

butaixianran opened this issue Feb 9, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@butaixianran
Copy link

butaixianran commented Feb 9, 2023

Describe the bug

At python side, TextBox can not get value, if the value is filled by javascript.

But if I type value into that textbox, python side can get value. I also tried to trigger change event in javascript, but python side still takes no action.

Is it a bug or it is designed in this way?
How can I make python side get the value filled by javascript?

Really need a solution here, thanks.

Is there an existing issue for this?

  • I have searched the existing issues

Reproduction

At Python side:
prompt = gr.Textbox(label="Prompt", lines=3, value="", elem_id="pt_prompt")

At javascript side:

let pt_prompt = gradioApp().getElementById("pt_prompt").getElementsByTagName("textarea")[0];
pt_prompt.value = "Som text";

The value is filled into textbox just fine with javascript. But python side can not get it. Unless I manually click into that textbox and type something.

Screenshot

No response

Logs

No log is needed here.

System Info

Windows 11, chrome latest, gradio 3.16.2

Severity

annoying

@butaixianran butaixianran added the bug Something isn't working label Feb 9, 2023
@butaixianran
Copy link
Author

butaixianran commented Feb 9, 2023

Also, is there a way to trigger html element's event at python side manually? If gradio won't trigger it automatically.

@pngwn
Copy link
Member

pngwn commented Feb 9, 2023

This is expected because of the frontend's reactivity works, it is not a bug.

In order for the changes to propagate, you need to dispatch an input event on the element too.

If you do this in your JavaScript then it should work:

const el = document
  .getElementById("pt_prompt")
  .getElementsByTagName("textarea")[0];
const input_event = new Event('input');

el.value = 'hello friends';
el.dispatchEvent(input_event)

This isn't recommended and there are no guarantees it will continue to work as we do not support this as official API but it should work for the foreseeable future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants