Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery .val() or .attr() not working correctly
    primarykey
    data
    text
    <p>I created a form in which you can create of edit an existing blog post. The form is initially hidden, and when you select "Create post", it shows the form blank, and if you click "Edit Post", it uses Ajax to load the post information and fill in the blanks. The problem is, when you switch between the two, some elements are not getting cleared and replaced everytime. Here is my JS script below:</p> <pre><code>&lt;!-- Hidden Create / Edit form --&gt; &lt;div id="news-form" title="" style="display: none;"&gt; &lt;div id="js_news_message" style="display: none;"&gt;&lt;/div&gt; &lt;form id="news" class="form" action="{URL}/admin/blog" method="post"&gt; &lt;input type="hidden" name="action" value=""&gt; &lt;input type="hidden" name="id" value=""&gt; &lt;p&gt; &lt;label for="title"&gt;Blog Title&lt;/label&gt; &lt;input id="title" name="title" class="required" type="text" value="" /&gt; &lt;/p&gt; &lt;div&gt; &lt;label for="body"&gt;Blog Body&lt;/label&gt; &lt;textarea id="body" name="body" class="tinymce" rows="50" cols="40"&gt;&lt;/textarea&gt; &lt;/div&gt; &lt;div class="clear"&gt;&lt;/div&gt; &lt;p&gt; &lt;a class="button red" id="reset-form" href="javascript:void(0);"&gt;Reset Changes&lt;/a&gt; &lt;input id="submit" type="submit" class="button" style="width: 150px; text-align: center; margin: 10px; float: right;" value="Submit"&gt; &lt;/p&gt; &lt;/form&gt; &lt;/div&gt; &lt;script src="{TEMPLATE_URL}/js/mylibs/jquery.form.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $("#create").click(function() { // Reset form elements $("input[name=action]").val('create'); $("input[name=title]").val(''); $("textarea[name=body]").val(''); // Show form, and hide any previous messages $('#js_news_message').attr('style', 'display: none;'); $('#news').attr('style', ''); $('#news-form').attr('title', 'Create News Post'); $('#news-form').dialog({ modal: true, height: 600, width: 750 }); // ============================================ // Editing News $(".edit").live('click', function(){ var news_id = $(this).attr('name'); //alert( news_id ); // Get our post $.post("{BASE_URL}/admin/news", { action : 'getpost', id : news_id }, function(result){ if(result !== 0) { var obj = jQuery.parseJSON(result); // Reset form elements $("input[name=action]").val('edit'); $("input[name=id]").val(news_id); $("input[name=title]").val(obj.title); $("textarea[name=body]").val(obj.body); // Show form, and hide any previous messages $('#js_news_message').attr('style', 'display: none;'); $('#news').attr('style', ''); $('#news-form').attr('title', 'Edit News Post'); $('#news-form').dialog({ modal: true, height: 600, width: 750 }); </code></pre> <p>Thats not the complete JS, but you get the idea. When the user hits the "Edit" button next to the blog post, then its brings up the jQuery Modal and the post information is already set to edit, and when they click "Create", it brings up the Modal again, with the blank form.</p> <p>Again, for some reason, sometimes everything will reset when swaping between edit and create, other times it will not. Anyone have any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload