0

Why am I getting the following error below:

Uncaught TypeError: Object [object global] has no method 'data'

HTML:

$attributes = array('class' => 'main post', 'onsubmit' => 'validateModules(event)', 'dataid' => $moviesparx_website_id); echo form_open("admin/websites/{$website_id}/page/op/{$action}/{$id}", $attributes); ?>

    <ul style="width: 25%; border-right: 1px solid #ccc; padding-right:10px;" class="postForm">
        <li>
            <input style="width:100%;" placeholder="Page Title" id="post_title" name="post_title" onclick="urlCheck()" dataid="<?php echo website_id;?> type="text" value="<?php echo set_value('post_title', $post['post_title']); ?>" />
            <?php echo form_error('post_title'); ?>
        </li>

JS:

function urlCheck()
{
      var id = self.data("id");

        $(".postForm").on('click', '#post_title', function(e){
        e.preventDefault();
        $.ajax({
           url      : '<?=base_url()?>/page/op',
           data     : { post_title : $("#post_title").val(), 'website_id' : id },
           type     : 'POST',
           success  : function(resp){
                alert( resp );
           },
           error    : function(resp){
                console.log("Error in ajax request");
           }
        });
    });
};
3
  • Is the problem that the click event handler is never called, or that the ajax request is returning something unexpected? Also, it may be more helpful to post the generated HTML, JS instead of the server side. Commented Oct 23, 2013 at 18:21
  • @makla Thanks - its because its never being called if I could solve this I should be sweet with the serverside "info" Commented Oct 23, 2013 at 18:25
  • The problem is line 3 of the JS snippet. It does not appear that 'self' is a jQuery element. Therefore .data() does not exist. Commented Oct 23, 2013 at 18:43

1 Answer 1

1
function urlCheck() {
    var id = self.data("id");

    $(".postForm").on('click', '#post_title', function (e) {
        e.preventDefault();
        $.ajax({
            url: '<?=base_url()?>/page/op',
            data: {
                post_title: $("#post_title").val(),
                    'website_id': id
            },
            type: 'POST',
            success: function (resp) {
                alert(resp);
            },
            error: function (resp) {
                console.log("Error in ajax request");
            }
        });
    });
}

urlCheck(); <-- did you add this line ?
Sign up to request clarification or add additional context in comments.

5 Comments

Yes I did - my issue is that its never being called
Thanks I have changed the question as I worked that out :) you able to help?
Correct but why if its set in the html as dataid?
Please see html as I have changed it so the dataid is with the input. I have also tried var id = $(this).data("id"); but I am getting undefined
@JessMcKenzie, shouldn't it be data-id, not dataid? dataid is not a valid data attribute.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.