Update JavaScript variable in jQuery

1.4k Views Asked by At

Please can someone help on how to get the value from jQuery into Javascript?

var myTitle = "abctitle";

function showtitle(myVar) {
    myTitle = myVar;    
    alert(myTitle); 
}

$(document).ready(function() {
    alert(myTitle); //I would like to alert "abctitle"
    myTitle = "changed";
});

how can i get the value set in query outside its call in jscript?

1

There are 1 best solutions below

1
On BEST ANSWER

Your code is right, and does what its supposed to

var myTitle = "abctitle";

var e = document.getElementById('show');
e.onclick = showTitle;

function showTitle(){
 alert(myTitle);   
}

$(document).ready(function() {
    alert(myTitle); //I would like to alert "abctitle"
    myTitle = "changed";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<button id="show">Click Me</button>

this will alert myTitle then the new title, showing that 'jquery' can access javascript variables and change them.