4

I'm using this: http://terminal.jcubic.pl/

I want to output something, and have that output be a hyperlink that when clicked would output something else.

    jQuery(document).ready(function($) {
        $('#terminal').terminal(function(cmd, term) {
             term.echo('\nClick this: <a onClick="javascript:terminal.exec(\'somethingelse\')">somethingelse</a>', {raw: true});
        }, {
            greetings: "Welcome!\n",

            onInit: function(terminal) {
                terminal.echo('Welcome!');
            },
            prompt: '> '
        });
    });

However,

<a onClick="javascript:terminal.exec

doesn't work because terminal.exec isn't defined in the context of the window. What would be the proper way to do it?

Thanks!

2
  • 1
    Handle the onclick in JavaScript and not inline HTML, and define it where the function is defined in your code. Commented May 24, 2015 at 4:05
  • It's all within that function: $('#terminal').terminal(function(cmd, term) { so how would that work? Commented May 24, 2015 at 4:12

2 Answers 2

3

You can define the onclick handler in your JavaScript:

<!-- In your HTML -->
<a class="trigger-terminal" href="#">Terminal</a>

// In your JavaScript in the scope of terminal
$('.trigger-terminal').click(function() { terminal.exec(); });

Or you could sin and expose your terminal variable by attaching it to the global window object:

window.terminal = terminal;

Here are the two suggestions inline with your code:

// First suggestion

jQuery(document).ready(function($) {
    $('#terminal').terminal(function(cmd, term) {
         term.echo('\nClick this: <a onClick="javascript:terminal.exec(\'somethingelse\')">somethingelse</a>', {raw: true});
    }, {
        greetings: "Welcome!\n",

        onInit: function(terminal) {
            $('.trigger-terminal').click(function() { terminal.exec(); });

            terminal.echo('Welcome!');
        },
        prompt: '> '
    });

// Second suggestion

jQuery(document).ready(function($) {
    $('#terminal').terminal(function(cmd, term) {
         term.echo('\nClick this: <a onClick="javascript:terminal.exec(\'somethingelse\')">somethingelse</a>', {raw: true});
    }, {
        greetings: "Welcome!\n",

        onInit: function(terminal) {
            window.terminal = terminal;
            terminal.echo('Welcome!');
        },
        prompt: '> '
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Neither of those seemed to do anything. There weren't any errors.
Update has the solutions embedded in the code you provided.
terminal is a property of $("#terminal"); it does not exist as a reference itself. IE : $('#terminal').terminal <-!
0

Variable can t be access. Also take care that terminal is a property of $('#terminal")

Solution 1

jQuery(document).ready(function($) {
    $('#terminal').terminal(function(cmd, term) {
         term.echo('\nClick this: '+$('<a></a>').text('somethingelse').on('click', function(){$('#terminal').terminal.exec();}), {raw: true});
    }, {
        greetings: "Welcome!\n",

        onInit: function(terminal) {
            terminal.echo('Welcome!');
        },
        prompt: '> '
    });
});

Solution 2

jQuery(document).ready(function($) {
    $('#terminal').terminal(function(cmd, term) {
         term.echo('\nClick this: <a onClick="javascript:$("#terminal").terminal.exec(\'somethingelse\')">somethingelse</a>', {raw: true});
    }, {
        greetings: "Welcome!\n",

        onInit: function(terminal) {
            terminal.echo('Welcome!');
        },
        prompt: '> '
    });
});

wrote on the fly, i hope it is correct. But the idea is there.

1 Comment

Just so this signature $('#terminal').terminal(function(cmd, term) { maybe you hsould use term and not terminal, but that is related to your library documentation, right ?

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.