1

I am new in java script and now I am stuck with a little problem. I searched over the internet but I did not get the answer so I have to post the question.

Problem: I wrote var x=1; in jack.js and I linked it with my HTML file. Now I want to call that var x=1; in HTML page then I want to change it to var x=2;. So that the result will be shown 2 not 1 in HTML page.

Can anybody suggest me what should I need to do?

1 Answer 1

3

The variable will be available within the document, so just include a <script> tag within the HTML.

<script>
x = 2;
document.write(x);

// or:
$('#targetDiv').text(x);
</script>

Note this has to be after you include jack.js.

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

5 Comments

thanks for reply. I want to ask you that If I want to save that x=2 in that jack.js file from html? then what i need to do?
The code in the js file will receive whatever the value of x is during execution. So for code that runs on page load, x will be 1 because HTML below has not yet changed it. Code that runs later, like a button click event handler, will see the new value of x.
thanks for reply.I got it now but I want to know that can we change any attribute value in a jack.js file forever from html page?
Sorry, I'm not sure what that means. You can change any global variable like you did for x. But if the js file has some variables within limited scope (like in a function or encapsulation) these aren't accessible outside of the scope and you will not be able to change them from within the HTML file.
no problem, thanks for help. I tried it and its working. thanks again

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.