Wilson212
Wilson212

Reputation: 563

jQuery .val() or .attr() not working correctly

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:

<!-- Hidden Create / Edit form -->
<div id="news-form" title="" style="display: none;">
    <div id="js_news_message" style="display: none;"></div>
    <form id="news" class="form" action="{URL}/admin/blog" method="post">
        <input type="hidden" name="action" value="">
        <input type="hidden" name="id" value="">
        <p>
            <label for="title">Blog Title</label>
            <input id="title" name="title" class="required" type="text" value="" />
        </p>

        <div>
            <label for="body">Blog Body</label>
            <textarea id="body" name="body" class="tinymce" rows="50" cols="40"></textarea>
        </div>

        <div class="clear"></div>
        <p>
            <a class="button red" id="reset-form" href="javascript:void(0);">Reset Changes</a>
            <input id="submit" type="submit" class="button" style="width: 150px; text-align: center; margin: 10px; float: right;" value="Submit">
        </p>
    </form>
</div>

<script src="{TEMPLATE_URL}/js/mylibs/jquery.form.js"></script>
<script type="text/javascript">

$("#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 });

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.

Again, for some reason, sometimes everything will reset when swaping between edit and create, other times it will not. Anyone have any ideas?

Upvotes: 0

Views: 1432

Answers (3)

Rafay
Rafay

Reputation: 31033

pass a function to .val and clear the value

$("textarea[name='body']").val(function(){    
return "";    
});

here a fiddle http://jsfiddle.net/dwqFb/61/

Upvotes: 0

Make it called on document ready:

$(document).ready( function() { // your code } );

Sounds to me like it isn't being set up once your document is ready...


Okay, second attempt. It may make no difference whatsoever, but I alway write the attr selectors with quotes like

$("input[name='example']")

Upvotes: 1

Jonathan Edgardo
Jonathan Edgardo

Reputation: 513

Looking at the line:

$.post("{BASE_URL}/admin/news", { action : 'getpost', id : news_id }, 

I can see that you got an error, because you can not recognize javascript that variable

I think you should better use:

var base_url = "{/literal}{BASE_URL}{literal}";

And Modify the line

$.post( base_url + "/admin/news", { action : 'getpost', id : news_id }, 

Upvotes: 0

Related Questions