8
$\begingroup$

This example displays text that changes from green to red when clicked:

DynamicModule[{col = Green},
  EventHandler[
    Style["first second third fourth", FontColor -> Dynamic[col]], 
    {"MouseDown" :> (col = col /. {Red -> Green, Green -> Red})}]]

I want text where the word "second" change from to red when the mouse is over it.

$\endgroup$
0

3 Answers 3

9
$\begingroup$

Perhaps I've missed something, but I see no reason to use low-level functions like EventHandler. I think you can use Mouseover at top-level to accomplish what you trying to do.

grn[txt_String] := Style[txt, Darker[Green]]
red[txt_String] := Style[txt, Red]
Row[{grn["first "], Mouseover[grn[#], red[#]]&["second "], grn["third fourth"]}]

With this code "second" changes from green to red when the mouse cursor it moved over it.

result

$\endgroup$
5
  • $\begingroup$ was of very great help, thanks $\endgroup$
    – Andres
    Commented May 15, 2018 at 11:46
  • $\begingroup$ @Andres. I'm glad you find my answer useful. Please consider accepting it. You can do that by clicking on the check mark that appears on the left of the answer below the down arrow. $\endgroup$
    – m_goldberg
    Commented May 15, 2018 at 17:30
  • $\begingroup$ Sorry, but now, if the text was more big. This code can result: Row[Dynamic@ Style[#, If[CurrentValue["MouseOver"], Red, Green], 48, "Section"] & /@ {"Experiencie", "leads", "us", "to", "believe", "that", "the", "motions", "of", "physical", "bodies", "are", "controlled", "by", "interactions", "between", "them", "and", "their", "surroundings"}, Spacer[5]] $\endgroup$
    – Andres
    Commented May 15, 2018 at 18:20
  • $\begingroup$ @Andres. I afraid you lost me with your last comment. I don't have a clue of what kind of response you expect me to make to it. It looks to me that it would be better addressed to kglr. $\endgroup$
    – m_goldberg
    Commented May 16, 2018 at 4:55
  • $\begingroup$ I was wrong, excuse me. Was to kglr $\endgroup$
    – Andres
    Commented May 16, 2018 at 13:05
9
$\begingroup$
Row[Dynamic @ Style[#, If[CurrentValue["MouseOver"], Red, Green], 48, "Section"] & /@ 
  {"first", "second", "third"}, Spacer[5]]

enter image description here

$\endgroup$
5
  • $\begingroup$ was of very great help, thanks $\endgroup$
    – Andres
    Commented May 15, 2018 at 11:46
  • $\begingroup$ Sorry, but now, if the text was more big. This code can result: Row[Dynamic@ Style[#, If[CurrentValue["MouseOver"], Red, Green], 48, "Section"] & /@ {"Experiencie", "leads", "us", "to", "believe", "that", "the", "motions", "of", "physical", "bodies", "are", "controlled", "by", "interactions", "between", "them", "and", "their", "surroundings"}, Spacer[5]] $\endgroup$
    – Andres
    Commented May 15, 2018 at 18:22
  • $\begingroup$ @Andres, if you want to keep it in a single row, use a smaller font size (e.g. change 48 to 12) and smaller spacing (e.g, change Spacer[5] to Spacer[1]). $\endgroup$
    – kglr
    Commented May 15, 2018 at 18:43
  • $\begingroup$ Is the text was more big. This is the code: text = "Experiencie leads us to believe that the motions of physical bodies are controlled by interactions between them and their surroundings"; StringSplit[text]; Row[Dynamic@ Style[#, If[CurrentValue["MouseOver"], Red, Green], 48, "Section"] & /@ StringSplit[text], Spacer[5]] $\endgroup$
    – Andres
    Commented Jun 12, 2018 at 19:25
  • $\begingroup$ Andres use Row[Dynamic@ Style[#, If[CurrentValue["MouseOver"], Red, Green], 12, "Section"] & /@ StringSplit[text], Spacer[1]] $\endgroup$
    – kglr
    Commented Jun 12, 2018 at 21:32
6
$\begingroup$

How about this?

DynamicModule[{col = Green}, EventHandler[
  Row[{
    Style["first ", FontColor -> Green],
    Style["second", FontColor -> Dynamic[col]],
    Style[" third fourth", FontColor -> Green]
    }],
  {"MouseDown" :> (col = col /. {Red -> Green, Green -> Red})}]]
$\endgroup$
4
  • $\begingroup$ Perfect. But, If I positioned myself on the word that I wanted, some code would also be possible. $\endgroup$
    – Andres
    Commented May 14, 2018 at 22:09
  • $\begingroup$ I'm not sure what you mean. What do you mean, "some could would also be possible"? $\endgroup$
    – march
    Commented May 14, 2018 at 22:44
  • $\begingroup$ If I place the mouse on the word "first" it turns red when I click. $\endgroup$
    – Andres
    Commented May 14, 2018 at 23:49
  • $\begingroup$ was of very great help, thanks $\endgroup$
    – Andres
    Commented May 15, 2018 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.