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
p5.Element::mousePressed()'s callback fails to use HTMLElement::setSelectionRange() #1814
Comments
Hi, It seems that this is not so much a p5 problem but JS event bubbling. My guess is that browser is bubbling mousedown event automatically for selection. With click this is more straight forward. To fix this, you need to stop mousedown event from bubbling. In your example:
You already prevented event from bubbling. When you remove "return false" It stops working. Here is your working example: var button_parse;
function parse_pressed(e){
// do some validations....and selecte what needs to be highlited
// call function to highlight
e.preventDefault();
selectCup();
return false;
};
function setup() {
noCanvas();
button_parse = createButton('Parse Content');
button_parse.mousePressed(parse_pressed);
};
function selectCup() {
console.log('hello');
var input = select("#result");
input.elt.setSelectionRange(0,3); // Highlights "Cup"
input.elt.focus();
}; |
@lmccart No this will break. selectCup function is out of event scope. And not returned back to event. |
oops thanks! I was misreading the flow of the original code. @feedzh you are correct, please disregard my snippet and go with one of the options posted. removing it now.. |
np. Happens with best of us :P |
I've tried to use p5.Element::mousePressed() in order to call a funtion that uses HTMLElement::setSelectionRange() to select a range within a TextArea, but it does not work.
I am new to p5.js so I posted a question on the forum. Luckly GoToLoop answer it and he was able to verify that it seems to be failing. He suggested to replace mousePressed() to some other event as a workarround.
I end up using mouseClicked() and it works.
Here is my code that is not working.
If you click on "Select cup it works, It selects "Cup" on the textarea.
If you click on Parse Content it does print "hello" on the console. but there is no selection on the textarea.
index.html
sketch.js
The text was updated successfully, but these errors were encountered: