How to change the class of an HTML element on a website - javascript

I'm studying on a website that reads with a super slow TTS the questions and answers and you can only skip to the next question when the TTS is done. I found that the button has a "nolink" class and that if i change it manually to a "link" class, i can answer the question right away.
I tried to search online how to make a tampermonkey script that keeps the button on the "link" class but none of what i have tried works and i don't have much knowledge with html or js.
I also tried to edit the .js of the website without much success either.
I'm looking for any solution that allows me to change the variable without editing the html every question if it is even possible.

(function() {
'use strict';
// Select the button element
const button = document.querySelector('.nolink');
// Check if the button element is found
if (button) {
// Change the class to "link"
button.className = "link";
}
})();

Related

Target the css class with some JS

Hello I currently have this inline JS on my wordpress navigation menu
login
I was told it is better to use a regular menu item then just give it a class then target the class with some JS. I tried searching for examples but can't find that works for me. Could someone share a sample code to point me in the right direction?
thanks
I think this is what you are told.
var link = document.querySelector(".js-link");
link.addEventListener("click", (e) => {
e.preventDefault();
//do whatever you want hear
console.log("redirect to another page");
});
Google
The way you do will make your code messy, hard to read and maintain. So we should separate html, css and js code. And to make your javascript and style not affect each other when you change your code, you should naming the class which you want to use javascript different with class you want to style.
For example I using class "js-link" just for javascript, not to style it. If I want to style the link I will add another class for it.

Javascript help for a Chrome extension

First of all, I am not a programmer. I do not know Javascript at all. I'm trying to create a Chrome extension that modifies my browser tab's title, by taking a specific string on a webpage. I know how to create Chrome extensions (just barely) and I just need to modify the Javascript to do what I want (which I do not know how).
I found the following script online and am trying to modify it but can't figure out how to get it working. Here is the script:
https://pastebin.com/dY1LSdjT
// Fire this event any time the mouse is moving. Sucks for performance, but it's a better experience
document.addEventListener("mousemove", function() {
// This will get us the banner
let bannerTextElements = document.getElementsByClassName("dijitReset dijitInputField dijitInputContainer");
console.log(bannerTextElements);
console.log(bannerTextElements[0]);
if (bannerTextElements[0]) {
console.log("ok!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
console.log(bannerTextElements[0].innerHTML);
// This will get us the text
let bannerTextLine = bannerTextElements[0].getElementsByClassName("dijitReset dijitInputInner");
console.log(bannerTextLine);
document.title = bannerTextLine[0].innerHTML
}
}, false);
And here's the website viewed in developer mode. I would like to get the text "blahhhh" and use that for the title of my browser tab.
Below is a different webpage and the difference here is that the "div class" names will change. The numbers in those class will change to different numbers, but the structure and the overall name remains the same. For example, "ms-Button-label label-549" may change to "ms-Button-label label-640". This happens whenever you refresh the page. I found that the "viewport" id name doesn't change though, so I think if we can use that as a reference and then just look at the nested div classes and reference them and extract the value (in this case Crystal Mountain).
Fixing your code
You got like 95% of everything you need.
The one thing you are missing right now is that the fact that an input element does not posses an "innerHTML" (That is why it isn't closed like this: <input>renderedText</input>). Instead you want to check for it's value:
document.title = bannerTextLine[0].value;
Some Improvements:
Search by ID
You have a well defined input which can be more easily obtained by looking it up using the id:
let bannerTextLine = document.getElementById('lanDeviceIndex_searchBox');
End result:
const bannerTextLine = document.getElementById('lanDeviceIndex_searchBox');
bannerTextLine.addEventListener("blur", function() {
if (bannerTextLine != null) {
document.title = bannerTextLine.value;
}
});
This is the entire JS code. Note that I changed the event to blur which activates when the form is left, this should be optimal for your case.

How to be able to click on a TR and edit personalia

I'm studying webdevelopment and I'm doing a single page application right now. We are using JavaScript, and I can`t use jquery, bootstrap, etc. I have googled, seen the videos from the lectures, but I am still blank as a canvas.
The problem is I need to make a contactregistre. You should be able to click on the contacts, a different section of the page should be made active where you will be able to edit the contacts and see where they live. The map is OK, but I don`t know how I can make this happen, I find no examples about this which does not suggest using jquery.
We have guessed something like this, but it is probably wrong:
document.querySelector("tr").addEventListener('click' , e => {
document.querySelector('editContact')
function editContact(contact) {
let editContact = document.querySelector("#searchcontact tr");
editContact.innerHTML = "TR";
let form = document.editContact()
}
})
Thanks so much in advance!
There are numerous ways to do this. This is not a complete solution, but should get you started:
Typically you'd have a text input already in your table <input name="username_1" class="hidden" type="text">, which is hidden, along side the content, like <span id="username_1">MAREN</span>
Then you'd have some CSS to hid things:
.hidden {
display:none;
}
So on the click even you'd add the hidden classname to the SPAN and remove it from the INPUT. This visually swaps the static value for the label.
There's more to it after that, but give it a try.

Hiding / displaying content in javascript

I am a very new web developer. I'm doin fine with HTML and CSS and im trying to learn javascript. Im very new 1 month ago I never coded a single line, and I've made 2 or 3 websites already but none are JS capable.
I know I can do this in jquery and have already, but I want to Understand it and I want to be able to do it in vanilla javascript.
So here it is . I am making a simple game for practice, all it does is ask questions and change elements from display = none to display = block based on a click event to two big YES or NO buttons.
So.... this first code targeting yes_button works fine. The instructions Hide and the first questiong (Q1) appears.
the No button does not work, and I cannot figure out why, it is essentially the same code targeting a different element.
Things I checked :
the element id/selectors are correct
camelCase is good,
no unclosed braces,
semi colons are good,
is the mistake in the logic somewhere?
window.onload = function() {
yes_button.onclick= function() {
document.getElementById("instructions").style.display="none";
document.getElementById("q1").style.display="block";
};
no_button.onClick=function() {
document.getElementById("instructions").style.display="none";
document.getElementById("nointro").style.display="block";
};
};
You can remove the window.onload function call. You can place your JavaScript imports just before the closing tag so the dom is ready before you attempt to click on the button.
Your JavaScript Events should look like this:
document.getElementById("yes_button").onclick = function(){
document.getElementById("instructions").style.display= "none";
document.getElementById("q1").style.display = "block";
};
document.getElementById("no_button").onclick = function(){
document.getElementById("instructions").style.display="none";
document.getElementById("nointro").style.display="block";
};

iTop, where to implement onchange javascript?

the iTop fourms are pretty inactive and I am pretty stuck so I thought I'd ask here if that is ok. I am attempting to add my own piece of javascript to work onchange of the iTop organization select bar.
Here is a link to iTop if needed: http://www.combodo.com/spip.php?page=rubrique&id_rubrique=8
Extra info on my iTop forum question, no answers yet: https://sourceforge.net/p/itop/discussion/922360/thread/dd1da92f/
So iTop is a piece of software made using php, html, js and css but I am new to php so I have ran into a little bit of a problem. I am trying to implement a piece of javascript which will change the top-bar of the css to a different colour based on organization, I made a small prototype independent of iTop and it works perfectly. However I am unable to add the onchange() method along with the javascript as the main user interface is generated dynamically using php. I have tried numerous ways to implement the javascript but none have worked. Here are some pictures of iTop and code to help:
This is a picture showing the inspect element of iTop and how I came to believe jquery-1.7.1.min.js may be the correct location to place the code.
So I have tried going into this file and copy and pasting in my Java Script code but in some locations it has no affect and within function(a){....} it results in iTop only displaying a blank page, I could be going about this totally wrong but from what I have seen this file seems to be the one I am to edit to get what I want.
here is the javascript i am trying to implement:
var changeStyle = function(){
//HTML
var selectedValue = document.getElementById("org_id").value;
//CSS
var trimColor = document.querySelector("#top-bar");
//BT
if(selectedValue=="3"){
//change banner colour
trimColor.style.backgroundColor= "#3498db";
}
//Bell Aliant
else if(selectedValue=="27"){
//change banner colour
trimColor.style.backgroundColor= "#f1c40f";
}
//Bell Canada
else if(selectedValue=="26"){
trimColor.style.backgroundColor= "#e74c3c";
}
else
trimColor.style.backgroundColor="#ecf0f1";
}
Here are some pictures of it working in my non iTop version of just HTML, JS and CSS:
This is an old question but I'll answer it anyway as the answer is still ok for the last versions of iTop.
To inject some JS in the page, you have to use the iPageUIExtension::GetNorthPaneHtml(iTopWebPage $oPage) API which gives you acces to the iTopWebPage object. Then you can use the various public methods such as "iTopWebPage::add_ready_script($sScript)" to inject $sScript into all of the backoffice pages. Moreover, the script will be executed once the DOM is ready.
You can find an example here.
class PageUIExtension implements iPageUIExtension
{
/**
* #inheritdoc
*/
public function GetNorthPaneHtml(iTopWebPage $oPage)
{
...
$oPage->add_linked_script(utils::GetAbsoluteUrlModulesRoot() . 'molkobain-handy-framework/common/js/handy-framework.js');
}
}
Note: You will need to make your own iTop extension, check here for how to do it.
Although I do not believe it is the best solution I have entered the following code into jquery-1.1.1.min.js as shown
And I have achieved the desired result, the trim now changes with the organization change, this however is not the perfect solution I was looking for and is a bit slow but not in the sense that you'd get annoyed, at least in my opinion. Mission Accomplished for the moment!

Categories