We are about to switch to a new forum software. Until then we have removed the registration on this forum.
First let me say that I am new to HTLM and Javascript..therfore also new to p5js. I found p5js very interesting so I started to create a page using p5js to parse messages from a Mainframe System. I need to peform some selection of text within a TextArea in the HTML, like show here http://jsfiddle.net/YNr7K/364/, using the setSelectionRange method of the TextArea. I don't find any way do it on p5js, so Is there a way to create another script function on the page like:
function selectCup() { var input = document.getElementById("TextAreaId"); input.setSelectionRange(3,5); // Highlights desired text, paramas will be variable. they are hardcode just for test. input.focus(); };
and call this function from within p5js? Thanks in adavance.
Answers
For p5.js sketches, the preferred way to deal w/ browser's DOM is via library "p5.dom.js":
http://p5js.org/reference/#/libraries/p5.dom
For example, instead of getElementById(), we use select(), which results on a p5.Element object:
If for some reason, we need to access the underlying HTMLElement, we access it via property elt:
http://p5js.org/reference/#/p5.Element/elt
GoToLoop, thanks for your response. i have something wierd going on here.
I have created this function in Sketch.js
my HTML has:
When i click on the button "Select Cup" on the HTML, the selectCup function is called on the sketch,js and the content is highlighted as expected. But when i try to call this function from within sketch,js nothing happens eventhough the "hello" gets printed on the console, so I know that the function was executed.
I've tried using select to access textarea "test" but when i do that, by that, I mean replacacing
with
I get this error. TypeError: input.setSelectionRange is not a function.
using
shows no error, but nothing happens, only Hello get printed on the log
Before anything else, learn how to post code in this forum: L-)https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Indeed, class p5.Element doesn't have any method called setSelectionRange().
As I had mentioned, if you need to access native HTMLElement members, do it via elt:
input.elt.setSelectionRange(0, 3);
What is supposed to happen? Disclaimer: I'm still a DOM noob myself. ~:>
I guess you're gonna need to post more of your sketch. :|
well.
What i mean is...using
when the function selectCup that IS defined within the sketch.js is called using the onmousedown event defined in the button Select cup withing the HTML, the function is executed, "hello" is printed on the console and the text "Cup" gets highlited as expected in the textarea.
but when I call the same function using the mousePressed event from p5.js using a button Parse Content I created calling createButton('Parse Content'); the function selectCup does not work, Nothing is highlithed eventhough "hello" is printed on the console.
Then...as you suggested I replaced
with:
with that change...Select cup is still working and Parse Content still does NOT work, "hello" gets printed on the console pressing on any button.
This is my sketch.js.
This is my HTML.
Is this an error?
Indeed you're right! Invoking selectCup() from p5.Element::mousePressed()'s callback parse_pressed() somehow fails to keep HTMLInputElement::setSelectionRange()'s highlighted text. @-)
But fret not, I've found a workaround for it! \m/
Just switch p5.Element::mousePressed() to another sibling event method. *-:)
For example: p5.Element::mouseReleased() or p5.Element::mouseClicked(). :D
You can also run my solution online at the link below: :bz
http://CodePen.io/GoSubRoutine/pen/qRzapj/top?editors=101
P.S.: New Version 1.1! :D
"index.html":
"sketch.js":
Thanks it works, do you think it needs to be reported?
Seems like a good idea: https://GitHub.com/processing/p5.js/issues ;))
https://GitHub.com/processing/p5.js/issues/1814
Yes, i know, this is my post :p
I see the p5.js' repo gave a solution for your bug issue now. <:-P
However, there are some wrong factual statements on @feedzh's answer though. 8-|
If that was so,
<input type=button value="Select Cup" onmousedown="return selectCup();">
woulda failed as well. :-@Also it's not related to bubbling propagation but event's default action cancelling. :-B
Again, it's not about bubbling-up propagation. [-X
Neither
return false;
nor Event::preventDefault() prevent events from propagating:https://developer.Mozilla.org/en-US/docs/Web/API/Event/preventDefault
If that's desired, we need to invoke Event::stopPropagation(): :P
https://developer.Mozilla.org/en-US/docs/Web/API/Event/stopPropagation
Anyways, if you take another look at my solution (now on v1.1), you'd see I'm applying another approach now:
createButton('Parse Content').elt.onmousedown = parsePressed;
After some further experiments, I've realized
return false;
only works for on*() event types.Like GlobalEventHandlers::onmousedown() for example:
https://developer.Mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onmousedown
Those registered via EventTarget::addEventListener() can only be cancelled exclusively via Event::preventDefault(): b-(
https://developer.Mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
As we can realize now, all of p5.Element's event registering methods rely on EventTarget::addEventListener().
That's the reason why HTML's input button worked w/
return false;
, while the p5.js version's failed!And apparently, neither p5.Element::mouseClicked() nor p5.Element::mouseReleased() demand Event::preventDefault() as much as p5.Element::mousePressed() does for your case though. :-?
Of course, the most reliable solution is to always use Event::preventDefault(), which works for all cases, rather than relying on
return false;
, which works only for GlobalEventHandlers::on*() event registering style. >-)