0

I´m having a normal JavaScript-function and want to use the Variable (myVar) also in my jQuery Code - is this possible? and how?:

<a onclick="showtitle(abctitle);" href="#">Testlink</a>

<script>
function showtitle(myVar) {
    myTitle = myVar;    
}

$(document).ready(function() {
    alert(myTitle); //I would like to alert "abctitle"
};
</script>
5
  • what is this I dont even Commented Apr 24, 2013 at 12:39
  • why is that variable in your HTML code in the first place? Commented Apr 24, 2013 at 12:40
  • how you get myTitle on your page load without click on a Commented Apr 24, 2013 at 12:42
  • @Sarfaraz with the current code, he can't Commented Apr 24, 2013 at 12:43
  • i think he has to improve his code first Commented Apr 24, 2013 at 12:44

3 Answers 3

3

Firstly, don't mix DOM0 inline event handlers with jQuery. Separate your markup and your logic.

If you use a data- attribute you can put your variable's content in your HTML, and then extract that in the event handler:

<a id="test" data-foo="mytitle" href="#">Testlink</a>

and then:

$(document).ready(function() {
    $('#test').on('click', function() {
        alert($(this).data('foo'));
    }
});

In this code the alert won't appear until the link is actually clicked on, of course.

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

4 Comments

this looks good, but I really want the showtitle()-function outside jQuery if possible!
@Marc why - what are you really trying to achieve? What do you mean by "outside jQuery"? jQuery is still just javascript.
because I start the showtitle()-function from a panorama player and this one needs the normal javascript-function: krpano.com/docu/js/#top
then you need to expand your question. Your existing code makes zero sense - you can't expect the document.ready handler to have access to a variable that doesn't get set until after you've clicked the link!
1

I believe @Alnitak has a great answer. But if you are just looking to solve the question you asked, wrap abctitle in single quotes and make myTitle a global variable:

<a onclick="showtitle('abctitle 2');" href="#">Testlink</a>

<script>
myTitle = "abctitle";

function showtitle(myVar) {
    myTitle = myVar;    
}

$(document).ready(function() {
    alert(myTitle); //I would like to alert "abctitle"
});
</script>

Also, your document ready function was missing its closing parenthesis )

Working example here: http://jsfiddle.net/CbhxY/

UPDATE

Working example on jsfiddle did not work so well. Try this: http://jsbin.com/ohedab/1/

The JS Bin example also adds the alert call in the showtitle function.

4 Comments

same comment as I made to mattytommo - the document.ready handler will get called automatically (with myTitle still undefined) before the onclick handler can ever be invoked.
Uncaught ReferenceError: showtitle is not defined (onclick on link)
I get that too, locally it works in a fresh html file...maybe an issue with jsfiddle and the way I'm declaring the fx?
@shanabus it's because jsfiddle has automatically wrapped your code in an onload handler, preventing showtitle from becoming a global variable (as is required by DOM0 inline event handlers).
0

you can do with this

function showtitle(abctitle){
  alert(myTitle); //I would like to alert "abctitle"
}

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.