# I simplified @Kirupa's webcam access in browser code by about 20%

**URL:** <https://forum.kirupa.com/t/i-simplified-kirupas-webcam-access-in-browser-code-by-about-20/655016>\
**Category:** web dev\
**Created:** [November 27, 2022, 8:43am UTC](https://forum.kirupa.com/t/i-simplified-kirupas-webcam-access-in-browser-code-by-about-20/655016 "2022-11-27T08:43:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakun-hu](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yakun-hu/32/19659_2.png) [@yakun-hu](https://forum.kirupa.com/u/yakun-hu)\
**Post date:** [November 27, 2022, 8:43am UTC](https://forum.kirupa.com/t/i-simplified-kirupas-webcam-access-in-browser-code-by-about-20/655016/1 "2022-11-27T08:43:15Z")

</div>

```auto
<p><video autoplay="true" id="videoElement"></video></p>
<button type="button" id="start" onclick="cam_on()">Cam-on</button>
<script> var video = document.querySelector("#videoElement");
function cam_on() {	stream = navigator.mediaDevices.getUserMedia({ video: true });
	stream.then(function (value) { video.srcObject = value; }); } </script>

```

Writeup on my [blog](https://www.wordpress.materialinchess.com/2022/11/27/h2s131-manipulatingin-html/):

I found @Kirupa’s code, [here](https://www.kirupa.com/html5/accessing_your_webcam_in_html5.htm), to be among the most concise that I’ve found; however, there was a slight logical fallacy with the .then implementation, which I addressed with a variable declaration rather conditional, as before. Hoping to hear @Kirupa’s thoughts.

---

<div class="post-metadata">

**Author:** ![steve.mills](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/steve.mills/32/14961_2.png) [@steve.mills](https://forum.kirupa.com/u/steve.mills)\
**Post date:** [November 28, 2022, 2:42am UTC](https://forum.kirupa.com/t/i-simplified-kirupas-webcam-access-in-browser-code-by-about-20/655016/2 "2022-11-28T02:42:56Z")

</div>

Hey Yakun-hu,  
I had a look at your blog and it appears that you have some templating issues…  
Are you using raw PHP or a framework?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [November 28, 2022, 5:53am UTC](https://forum.kirupa.com/t/i-simplified-kirupas-webcam-access-in-browser-code-by-about-20/655016/3 "2022-11-28T05:53:45Z")

</div>

Hi @yakun-hu - welcome to the forums! 😃

I looked at your code and mine, and I am not sure what the bug in my code is. Can you please elaborate?

Thanks,  
Kirupa 🐞
