# Code in nb does not work in cdf preview

**URL:** <https://community.wolfram.com/t/code-in-nb-does-not-work-in-cdf-preview/6427>\
**Category:** Questions\
**Tags:** education, computable-document-format\
**Created:** [August 14, 2015, 3:39pm UTC](https://community.wolfram.com/t/code-in-nb-does-not-work-in-cdf-preview/6427 "2015-08-14T15:39:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ejeh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ejeh/32/9140_2.png) [@ejeh](https://community.wolfram.com/u/ejeh)\
**Post date:** [August 14, 2015, 3:39pm UTC](https://community.wolfram.com/t/code-in-nb-does-not-work-in-cdf-preview/6427/1 "2015-08-14T15:39:30Z")

</div>

The following code (attach) works in nb, but doent work in the cdf preview. Why? Is it the MessageDialog?

---

<div class="post-metadata">

**Author:** ![jfriedman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jfriedman/32/35953_2.png) [@jfriedman](https://community.wolfram.com/u/jfriedman)\
**Post date:** [August 22, 2015, 6:58pm UTC](https://community.wolfram.com/t/code-in-nb-does-not-work-in-cdf-preview/6427/2 "2015-08-22T18:58:45Z")

</div>

Yes, I believe it is the MessageDialog. Here’s a version that should work, provided you have the latest version of CDF player:

```
Manipulate[
 Column[{Labeled[
    NumberLinePlot[{n}, PlotRange -> {{-6, 6}, {-1, 1}}, 
     Ticks -> {{0}}], "x", Right], 
   Style[If[c, "Correcto", "Incorrecto", ""], "Subtitle"]}], {{n, -4, 
   ""}, {-4, 3, 0, -2, 5}, Slider}, {{c, Null, ""}, 
  Row[{Button["Positivo", c = Positive[n]], 
     Button["Negativo", c = Negative[n]], 
     Button["cero", c = n == 0]}] &}]

```

---

<div class="post-metadata">

**Author:** ![ejeh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ejeh/32/9140_2.png) [@ejeh](https://community.wolfram.com/u/ejeh)\
**Post date:** [August 22, 2015, 7:02pm UTC](https://community.wolfram.com/t/code-in-nb-does-not-work-in-cdf-preview/6427/3 "2015-08-22T19:02:18Z")

</div>

Thank You. I think the second parameter of the instruction

```
Dynamic [p1 (p1 = #; MiFp [#]) &]

```

is not working in CDF. It’s a shame, because it provides a good window for better programming. For my purposes Manipulate seems limited.

I’ll wait to see if this alleged error is corrected in the future. If so is the case.

I attach a new version without MessageDialog. Which of course does not work in CDF.

Best regards

---

<div class="post-metadata">

**Author:** ![ianh](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ianh/32/14440_2.png) [@ianh](https://community.wolfram.com/u/ianh)\
**Post date:** [August 23, 2015, 1:52am UTC](https://community.wolfram.com/t/code-in-nb-does-not-work-in-cdf-preview/6427/4 "2015-08-23T01:52:58Z")

</div>

Your original example depends on a user evaluating the code in the notebook, which they cannot do in CDF Player. So to remove this requirement, you would need/want to use DynamicModule. And note the use of SaveDefinitions in the attached.

> **[DynamicModule—Wolfram Documentation](https://reference.wolfram.com/language/ref/DynamicModule.html)**
>
> DynamicModule\[{x, y, ...}, expr\] represents an object which maintains the same local instance of the symbols x, y, ... in the course of all evaluations of Dynamic objects in expr. Symbols specified in a DynamicModule will by default have their values...

Ian
