Pass a Variable Value from One HTML Page to Another

Поділитися
Вставка
  • Опубліковано 11 лис 2024

КОМЕНТАРІ • 115

  • @OpenJavaScript
    @OpenJavaScript  2 роки тому +2

    Thanks for watching!
    👉 Code for this tutorial: openjavascript.info/2022/06/09/pass-data-from-one-html-page-to-another/

    • @dgh25
      @dgh25 2 роки тому +1

      You dont need line 26, simply call the form as its id, no need for getelementbyid.

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      @@dgh25 Have to select the form somehow. What solution do you have in mind?

    • @dgh25
      @dgh25 Рік тому

      @@OpenJavaScript You are already "selecting" it by "form.addEventListener" where "form" is the id of the form.
      (I deleted line 26, and it still works.)

    • @feitan7712
      @feitan7712 Рік тому +1

      Why does the preventdefault function work internally but not externally

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      ​@@feitan7712 When you call e.preventDefault() it sets a property on the event object that is created (in the background) to false and that is used to prevent whatever default behavior has been set for the event.
      To trigger this property to be false, you must call preventDefault() on the event object that is made available to you because this was produced by the event and can set its 'defaultPrevented' property.
      If you are interested in looking into this more deeply, I created an entire tutorial on creating a custom event, including how to prevent a default behavior. Here's the relevant bit: ua-cam.com/video/47n53M3U4WA/v-deo.htmlm20s

  • @suzunyan00
    @suzunyan00 Рік тому +11

    My goodness, did you save me! Was struggling with this for the past few hours and all the other solutions were so unnecessarily complicated. Also, this was explained in such a simple way. Thanks a lot.

    • @OpenJavaScript
      @OpenJavaScript  Рік тому +1

      You are welcome, glad you found it useful :)

  • @castro4694
    @castro4694 Рік тому +6

    I was offered an internship if could finish a tech challenge in which I consume an API and part of the test was opening another page with the content from the search of the first one. This is exactly what I needed

  • @thucle4122
    @thucle4122 Рік тому +8

    Was struggling at 2a.m and then found your vid man, it's tremendously helpful for a beginner in the web dev field like me. Thanks you.

    • @OpenJavaScript
      @OpenJavaScript  Рік тому +1

      Been there myself a few times 😂
      So glad to hear the tutorial was able to help you out!

  • @xSyrious
    @xSyrious Рік тому +1

    This was exactly what I was looking for. I'm using multiple pages and a separate JS script file but I only had to accommodate a little to make things work. Thank you!

  • @LordKraken3
    @LordKraken3 7 місяців тому

    This was exactly the tutorial I'm looking for to complete a project for my Web Design class; thank you so much!

  • @andrews13
    @andrews13 Рік тому +2

    This is an underrated channel!

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      Thank you, I'm glad you like the content, I'll try to keep it coming :)

  • @therealshelly
    @therealshelly Рік тому +1

    I FREAKING LOVE YOU, YOU SAVED ME BRO

  • @chinnyi2384
    @chinnyi2384 3 місяці тому

    this was exactly what i needed THANK YOUU 🙏

  • @SamHodges-ht7xt
    @SamHodges-ht7xt Рік тому +1

    I'm Very thankful that you have made this tutorial. Helped me out a lot :)

  • @jitendrapuri
    @jitendrapuri Рік тому +2

    very articulate and well explained. Thank you.

  • @KatakamUmaNandini
    @KatakamUmaNandini 2 місяці тому

    Thank you very much. This is what I exactly need.

  • @CRCE__Lionel_Castelino
    @CRCE__Lionel_Castelino Рік тому +1

    thankyou !
    this video helped me in my project

  • @noneofurbusiness8809
    @noneofurbusiness8809 Рік тому

    this helped me to finally understand it!!

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      That's great! It's hard for me to gauge how helpful my videos are beyond views and likes. So really appreciate you taking the time to let me know that this helped you understand this. Thanks!

  • @jzurgegarcia7107
    @jzurgegarcia7107 2 роки тому +2

    Finally found this as it resolves my issue. Can you also create tutorial on concatenating selected values from 2 or more drop down, and checkbox and send to another HTML page

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      I was asked a similar question in one of the other comments...so I think I should do this. Coming soon!

    • @jzurgegarcia7107
      @jzurgegarcia7107 2 роки тому

      @@OpenJavaScript please do this master. hope very soon😄

  • @deepthikundanagurthi7203
    @deepthikundanagurthi7203 2 роки тому +3

    Finally found the solution , trying to do it from 2 days, thanks for the effort

  • @李增-i4l
    @李增-i4l Рік тому

    a tremendous help to me. I like your video a lot. Hope that you'll post more videos.

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      That's great to hear that you found this useful. Thanks for reaching out to let me know.
      There are lots more videos on the way, so if you haven't already, you might want to subscribe so you don't miss them :)

  • @Shinobu-qv7vd
    @Shinobu-qv7vd Рік тому +1

    So Good So Thank you for this video I would suck without this video

  • @Asurow
    @Asurow Рік тому +1

    I know session and local storage exists but was wasted unit this video. 😆Thank you.

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      Glad you found this useful! Thanks for reaching out :)

  • @Im_Pranav
    @Im_Pranav 2 роки тому

    Amazing the exact video i was searching for 🙏

  • @grejs6832
    @grejs6832 10 місяців тому

    Hi thank u so much! But why does it only save the first input for me? Even if i put a different input it remains the first input remains in the local storage

  • @NaVindu0315
    @NaVindu0315 Рік тому

    jst what i was searching for thanks a lot

  • @feitan7712
    @feitan7712 10 місяців тому

    I know its been a while but I have an issue with it which may be from my end:
    the value of localstorage is present in the console but is never put into the HTML file itself

  • @zarmr9719
    @zarmr9719 Рік тому +2

    First of all thank you for this great tutorial! But I have a question: is this going to work if the website is uploaded on the internet? If I use this method to change text on my website, will the new text appear for everyone who visit my website? Or do they have to be on the same device?

    • @zarmr9719
      @zarmr9719 Рік тому +2

      I am asking because when I try to run VS Code Server to test my html on phone, the changes always appear only on the device on which I made the changes.

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      Thank you, glad you liked the tutorial!
      The answer to your question is no, a value stored to local storage is local to a user's browser only. Any values stored there are not accessible by other users visiting the site.
      To have a value that changes for all users you need to store a global value somewhere outside the browser. You can do this by implementing a backend server with Node.js.
      Here I have a tutorial on how you can implement a simple counter in this way:
      ua-cam.com/video/mtuYmm2ITvI/v-deo.html
      And here's how you can implement such a server via a shared hosting account via cPanel:
      ua-cam.com/video/emDCYo5Rz0E/v-deo.html

  • @MrBenske4
    @MrBenske4 Рік тому

    great guide mate!
    im wondering if you could help me a bit. im following this guide but im trying to make both html pages i have interact with one javascript document. my issue is everytime it navigates to the next html page, all of my ids and variables from the previous page get ost and return null on the next page. ive used local storage for several things but for instance my variable holding the id for form gets lsot when it navigates to the next html document. if you have any suggestions that would be great.
    cheers

  • @prasunbiswas1790
    @prasunbiswas1790 10 місяців тому

    I am getting an error in the log saying , Cannot read properties of undefined (reading addEventListener) . What can I do???

  • @kyleritsuki
    @kyleritsuki 2 роки тому +1

    do have video for retrieved data from database like firebase?

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому +1

      Unfortunately not yet...but I will keep it in mind for a future tutorial ;)

  • @denichasinthabag
    @denichasinthabag Рік тому +1

    How can I tranfer image I tried everything and I always get default image and in console it says src null

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      Try this tutorial (saving an image to localSotrage):
      ua-cam.com/video/XZ8CjU9eaVA/v-deo.html

    • @denichasinthabag
      @denichasinthabag Рік тому +1

      @@OpenJavaScript I looked and changed my code and it works , thanks !

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      @@denichasinthabag Great!

  • @paulcal3500
    @paulcal3500 Рік тому +1

    Thanks for the vid. It's helpful, but is this the most efficient and easiest way to store variables with JS?

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      I'd say much easier to use than the alternative, IndexedDB, though some don't like the fact that local storage is blocking (synchronous). However, for most variable values, unless stringified files, the data is small enough that it is so quick to save and retrieve as to not really make a difference to user experience (note local storage only accepts string data, I have created a more general tutorial about LS here: ua-cam.com/video/-ZRDZyUjEEI/v-deo.html).
      However, if you do want to start storing large data items, such as files in their original format, you should take a look into IndexedDB (I don't have a tutorial on it yet but maybe I'll create one soon).

  • @SeyiAudu-cy5qc
    @SeyiAudu-cy5qc 15 днів тому

    Thanks!

  • @vijaysarathi2883
    @vijaysarathi2883 Рік тому +1

    Is it possible to get input data from a user page and run that data as a function in backend and display that output to another page ?

    • @OpenJavaScript
      @OpenJavaScript  Рік тому +1

      Yes, for that you'll want to do what I do in this tutorial: ua-cam.com/video/dFG2ikJr4U8/v-deo.html
      This shows you how to pass a function through localStorage but you can use this same technique to send one between frontend and backend.

  • @AAA-f3z1p
    @AAA-f3z1p Рік тому

    Great Vedio , thank you

  • @Lipunya
    @Lipunya 2 роки тому +1

    can you write an example with checkbox and radiobutton plz!!!

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      I will include more examples with these in my future videos :)

  • @EstherBose-wy4tc
    @EstherBose-wy4tc 5 місяців тому

    THANK YOUUUUUUUUUUUUUUUUUUUUU

  • @vinces.9148
    @vinces.9148 2 роки тому +1

    Is there a way to do this with a dropdown selection and dates? Say a hotel website or airline look up type of situation? I am thinking the index page has a form with 3 values (say location, start date, end date) and the second page has a reservation page with a form of 5 values that would be blank if loaded by itself. If loaded from the user selection on the index page, their choices would be preloaded, but still able to be changed, and the other options being blank. Thanks for all the great videos!

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      Yes, that is absolutely possible.
      You would need to save the values in localStorage or SessionStorage on the first page. Then on the next page, run some code immediately that would populate the values of the page with those of the previous one.
      I have been asked in another comment about this so will make a video on this soon!

  • @xxjz4034
    @xxjz4034 2 роки тому +1

    Thanks god i found you

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      Glad you found it useful! I remember when I first came across this issue. So frustrating trying to found a solution but thankfully there is one :)

  • @vanninany
    @vanninany Рік тому

    will it be possible to pull that local storage info into the html code and display it? if so how? or do you have a recommended video for that? thank you so much for the amazing content

    • @jakob.s3039
      @jakob.s3039 Рік тому

      I managed to get it to work with a constant declaration (const data = JSON.parse(...)), and then changing document.getElementByID("...").textContent = data.fn

    • @jakob.s3039
      @jakob.s3039 Рік тому +1

      so, inside of next.html, just add a const declaration for myObject, and then refer to the value fn in the record {fn: firstNameValue, ln: lastNameValue} (in index.html).
      for example:
      First Name:
      const data = JSON.parse(localStorage.getItem("myObject"));
      document.getElementByID("first-name").textContent = data.fn

    • @vanninany
      @vanninany Рік тому

      @@jakob.s3039 Excellent! You’re the best thank you so much :). One last thing - would the sessionStorage data appear if you were to have the button open up the next.html page on a separate window? (versus on the same window that index.html was initiated on)

    • @jakob.s3039
      @jakob.s3039 Рік тому

      @@vanninany No worries :) sessionStorage data does not seem to disappear when you go from start.html to next.html. I tried closing everything down, and when I came back to next.html, the data was still there locally :D It can be reset by clearing cookies (or opening up an incognito tab).

    • @marior5361
      @marior5361 Рік тому

      @@jakob.s3039 is there a way to save the value each time a button is click on a seperate html and display it. For some reason it only displays the most recent data instead of creating a new div with the new data while keeping the old one. Even tho I placed in code to create a new div with the new data.

  • @EOTStorm
    @EOTStorm 2 роки тому +1

    Thank you for this video. Helped me out tremendously.
    Is there a way to pass that variable on another HTML page that has an image? I would like the variable on top of the image. Thank you.

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      Yes, it is technically possible but it is only possible to store string data in local storage and it has limited space (usually about 5MB per site).
      You therefore have to 'stringify' the image first by creating a data URI to it (e.g. using FileReader). You then need to convert the image from the data URI back to a blob (you can find solutions for this online and also some libraries on npm).

    • @EOTStorm
      @EOTStorm 2 роки тому

      @@OpenJavaScript Well, okay then. lol Thank you

  • @nikoletakallou5257
    @nikoletakallou5257 2 роки тому +1

    can you please post somewhere the hole code pleasee?

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      Here you go: openjavascript.info/2022/06/09/pass-data-from-one-html-page-to-another/

  • @ByzaOCE
    @ByzaOCE 2 роки тому

    Great video mate, I'm new to all this, and the first example of displaying the passed local storage worked, but i see you haven't finished the code off for object and array to display what has been stored in first & last name fields, got me lost.

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому +1

      Ah, see what you mean there. Could have been clearer towards the end. Lots of commented out code and switching between pages.
      But glad you found it useful (in the end!)

    • @ByzaOCE
      @ByzaOCE 2 роки тому

      @@OpenJavaScript and displaying the result in the next page like you did for the first example. Thanks for taking the time to respond :)

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому +1

      @@ByzaOCE Thank you for the feedback! Appreciated.

  • @EWeaponsLab
    @EWeaponsLab 4 місяці тому

    Thanks 🎉

  • @FaceBook-zb6mx
    @FaceBook-zb6mx Рік тому

    Can you please explain how we can do this with a number input.
    How we can save number value and use it in another html page
    Thank you

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      localStorage only stores string data. It will try to coerce anything you store there to string.
      So if you store a number there, it will be a string. So when you retrieve, parse it from string to number:
      localStorage.getItem(Number(itemKey))

  • @torgeirullestad3921
    @torgeirullestad3921 2 роки тому +1

    does this work in cshtml as well? (don't laugh if it seems like a stupid question please) :)

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      Personally, I haven't used cshtml.
      But I *think* it should be fine because it finally renders to the page as HTML. So you should have access to HTML APIs also.

  • @tomislavbusic2652
    @tomislavbusic2652 2 роки тому

    Thank you!!!!

  • @asdffasdf3234
    @asdffasdf3234 2 роки тому

    thank you very much sir thank you again

  • @christnovs
    @christnovs 2 роки тому +1

    sir how about html page to php page? can u make a video about it? thanks

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому +1

      You can only access localStorage on the client-side with JavaScript (it is actually part of the HTML Web Storage API).
      If you want to send it to PHP, you'll need to write some JavaScript first to get the values and then send them as you would when posting something normally.

    • @christnovs
      @christnovs 2 роки тому

      @@OpenJavaScript Okay, Thanks

  • @julietferreira9031
    @julietferreira9031 2 роки тому

    Thank you soooo muchh!!!!!!!!

  • @hackerlurcker8675
    @hackerlurcker8675 5 місяців тому

    thankkkkkkkkk uu smmmmuch im soooooooooooooooooooooooooooooooo hapyyyyyyyyyyypyyppyppp

  • @anandsnair7409
    @anandsnair7409 2 роки тому +1

    thank you can you share the file pls

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому +1

      If you mean the source code, you can find it on my website: openjavascript.info/2022/06/09/pass-data-from-one-html-page-to-another/

  • @yasirmohammedali
    @yasirmohammedali Рік тому +1

    hi ... thank you

  • @NaVindu0315
    @NaVindu0315 Рік тому

    ❤❤❤❤❤❤

  • @LinusJohannesson-m9o
    @LinusJohannesson-m9o Рік тому

    Please use an external keyboard for your videos, the hammering on the keys steals attention from your otherwise great content.

  • @IslamicHub12
    @IslamicHub12 6 місяців тому

    Wow

  • @nokee092life4
    @nokee092life4 9 місяців тому

    i know this is old and that might be why but for me "window.location.href = example.html" diddnt work and i had to change "submit" in thy button type to "reset" and remove the "e.preventDefault" to make it work. else then that i love the toutorial it was exactly what i needed.

  • @AlexanderWilhite
    @AlexanderWilhite Рік тому

    • never trust a nerd to explain simple shit online