Submit your widget

jQuery images dynamic form preview

Created 9 years ago   Views 17037   downloads 2920    Author papermashup
jQuery images dynamic form preview
View DemoDownload 61.2 KB
Share |

a dynamic form preview tool for work so clients could preview how the text and images would look before submitting the content to be published. I’ve trimmed down the code and removed the database part completely from this example so we can concentrate purely on the JavaScript.

The JavaScript


function update(){		

var title = $("#title").val();
var photo = $("#photo").val();
$('#image').html('<img src="'+photo+'"/>');


<div class="left">
      Choose a site<br/>
      <select name="pic" class="click" id="photo">
        <option value="images/photo1.png"></option>
        <option value="images/photo2.png"></option>
        <option value="images/photo3.png"></option>
      Add a Tagline
      <textarea id="title" class="click" name="title" cols="40" rows="4">This is your default advert text. </textarea>
  <div class="right">
    This is where you could put a link for users who have JavaScript disabled to see a preview
    <div id="preview"> This is how your advert will look
      <div id="image"></div>
      <div id="Displaytitle"></div>

<div class="clear"></div>

In the dom ready function we’re simply hiding the div ‘#preview’ as this is our container for the contents of our form preview. next we setup the ‘.click’ and ‘.keypress’ listeners that execute that function ‘update()’ to display the preview of our form contents. Next looking at the ‘update()’ function we firstly display the ‘#preview’ div. Then set the variables ‘title’ and ‘photo’ with the values of the form using the ‘.val()’ attribute. Then to update our form preview we use ‘.html()’ to replace the contents of the selected div.

Using .html .append & .prepend

Its useful to know that ‘.html’ will replace the contents of the div with whatever you specify as shown below.


<div id="replace">This will be replaced</div>
$('#replace').html('with this text');

You can however use ‘.prepend’ and ‘.append’ to add the new contents before or after the current contents as shown below.


<div id="time">The time is: </div>


<div id="time"> is the time</div>

The article source: