Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Changing HTML tag attribute with JS

Featured Replies

This may be a really stupid way of going about this, but unfortunately it's the only thing I could come up with.

So for some odd reason the horizontal scrollbar is coming up on a site I'm making and I don't want it to be there, but I still wanted it there when people had smaller browser windows,so I thought I'd use some JavaScript!

 

I've currently got this code:

 

var winW = 630, winH = 460;
if (document.body && document.body.offsetWidth) {
winW = document.body.offsetWidth;
winH = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
   document.documentElement &&
   document.documentElement.offsetWidth ) {
winW = document.documentElement.offsetWidth;
winH = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
winW = window.innerWidth;
winH = window.innerHeight;
}
if (winW > 640){
document.getElementById("html").innerHTML( style='overflow-x:hidden');
$(":first").setAttribute(" style='overflow-x:hidden'");
}

 

Which is supposed to add style='overflow-x:hidden' to my HTML tag if the window width is greater than 640px... It doesn't.

 

Anyone got a fix (Or another way to go about fixing this problem)?

 

Thanks in advance :D

Hi there,

 

I think the problem is to do with the use of innerHTML which will expect a string to be passed. innerHTML will essentially inject an html string into the selected element:

 

// Example
document.getElementById("html").innerHTML = '<div id="test"></div>';

 

I haven't tested this but I'd say that the innerHTML in your script would cause an error which could result in nothing happening in your page.

 

You may also need to change the setAttribute piece too:

//from:
$(":first").setAttribute(" style='overflow-x:hidden'");

//to
$(":first").style="overflow-x:hidden";

 

I'm not 100% on this one though, my javascript is a little rusty.

 

I hope this helps.

  • Author

I actually fixed it ^_^

If anyone's interested about how to get the horizontal scroll bar on a site to only show up when the screen size is smaller than x use THIS:

 

var winW = 630, winH = 460;
if (document.body && document.body.offsetWidth) {
winW = document.body.offsetWidth;
winH = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
   document.documentElement &&
   document.documentElement.offsetWidth ) {
winW = document.documentElement.offsetWidth;
winH = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
winW = window.innerWidth;
winH = window.innerHeight;
}
if (winW<900) document.getElementById("html").removeAttribute("style")

 

Where the 900 on the last line represents the amount of pixels in width you want.

 

Hope that helps everyone that didn't know how (like myself ;) )

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.