Dollars BBS | Technology

feed-icon

Main

News

Animation

Art

Comics

Films

Food

Games

Literature

Music

Personal

Sports

Technology

Random

about JQuery (2)

1 Name: noob_coder : 2021-11-21 04:48 ID:0EIlt17S [Del]

can anyone help mw whith JQuery, can anyone translate $('select').change(function() {
var op =$(this).val();
if(op !='') {

$('button[name="processor_details"]').prop('disabled',false);
} else {
$('button[name="processor_details"]').prop('disabled', true);
}
});
to javascript.

Or could JQuery be translated at all?

2 Name: Name !Lup0uZudWo : 2021-11-21 13:50 ID:wi4NVFjG [Del]

$() is basically just document.getElement[s]By<Id/TagName/ClassName>. That [name=""] is just looping through all the button elements to find the element with the name attribute set to "processor_details". .prop is the exact same thing as .setAttribute. The $().change() is the same as element.change=. Finally, an empty $().val() is equivalent to getting the element.value.

However, because .disabled is a normal attribute of button elements, we don't need .setAttribute and can instead just do .disabled=. So, it'd be something like:

let selectElements = document.getElementsByTagName("input");
for(let i=0;i<selectElements.length;i++){
selectElements[i].onchange = ()=>{
let op = selectElements[i].value;
let buttonElements = document.getElementsByTagName("button");
for(let j=0;j<buttonElements.length;j++){
if(buttonElements[j].name == "processor_details")
buttonElements[j].disabled = op=="";
}
//From here until before the }; is not required if you do not need to do anything but set the buttons to disabled
if(op!=""){
//do stuff while buttons are enabled
}else{
//do other stuff while buttons are disabled
}
};
}