0

I have written my javascript too much, and now the code keeps repeating itself, whereas I lack of knowledge on how to simplify matters. I have this idea of calling variable into function, but I don't know how to call this kind of function that contains dynamic variables.

Anyone got any tips on how can I achieve this?

var container    = '#content_container';

function container_load(){
    var data         = $(this).attr('data');
    var dataObject   = {command : data};
    var title        = '<h2 data="'+dataObject.command+'">'+
                        dataObject.command+'</h2>';

};

$(function(){
    $('nav')on.('click', 'a', function(){
        container_load();
        $(container).prepend(title);
    });
});

Apparently, console returned ReferenceError: Can't find variable: dataObject

0

5 Answers 5

1

There is two issue is in your code

var container    = '#content_container';
var title;   //title should be declare as global,same as "container" variable
function container_load(dis){
    var data         = dis.attr('data');
    var dataObject   = {command : data};
    title        = '<h2 data="'+dataObject.command+'">'+
                        dataObject.command+'</h2>';

}

$(function(){
    $('nav').on('click', 'a', function(){
        container_load($(this)); //you have to pass the current element
        $(container).prepend(title); 
    });
});

Demo : Demo

Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

var container    = '#content_container';

function container_load(currElementId){
    var data = $("#"+currElementId).attr('data');
    return '<h2 data="'+data+'">'+data+'</h2>';
};

$(function(){
    $('nav')on.('click', 'a', function(){
         var title = container_load(this.id);
        $(container).prepend(title);
    });
});

Here your problem is that you cannot 'this' in other function for that you need to pass it from your current function.

Comments

0

There seems to be few mistakes in your code, The scope is wrong and the data attribute is used not correctly I presume. I suppose this is what you wanted http://jsfiddle.net/EjEqK/2/

HTML

<nav ><a href="#" data-val="abc">aaa</a></nav>
<div id="content_container"></div>

JS

function container_load() {
var data = $(this).data("val");
var dataObject = { command: data };
$("#content_container").prepend('<h2 data-val="' + dataObject.command + '">' + dataObject.command + '</h2>');
};

$(function () { $('nav > a').on('click', container_load); });

PS: If you don't need dataObject for anything else, directly use data

Comments

0

I think this will help you :

function container_load(currElement){
var data = $(currElement).attr('data');
return '<h2 data="'+data+'">'+data+'</h2>';
}

$(function(){
var container    = '#content_container';
    $('nav')on.('click', 'a', function(){
         var title = container_load(this);
        $(container).prepend(title);
    });
});

Comments

0

You could do the following :

var container = '#content_container',
    title; // make title global

function container_load() {
    var data = $(this).attr('data');
    var dataObject = { command: data };
    title = '<h2 data="' + dataObject.command + '">' + 
        dataObject.command + '</h2>';
};

$(function () {
    $('nav') on.('click', 'a', function () {
        container_load.call(this); // bind this to container_load
        $(container).prepend(title);
    });
});

But you could do even better :

$(function () {
    $('nav') on.('click', 'a', function () {
        var data = $(this).attr('data');
        $('#content_container').prepend(
            '<h2 data="' + data + '">' + data + '</h2>'
        );
    });
});

Comments

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.