Javascript to hide part of form on radio button change
Date : March 29 2020, 07:55 AM
wish helps you I have the below html form and need to write javascript to hide different parts of the form depending on the top radio button selected. , One approach is the following: function showRSVP() {
// caching the changed element (whose change event called the function):
var checked = this,
// getting the value of that changed element, and changing it to a
// number (in base-10):
n = parseInt(checked.value, 10),
// getting all the 'div' elements whose id starts with 'rsvp':
rsvpElems = document.querySelectorAll('div[id^="rsvp"]'),
// initialising a variable for use later:
tmp;
// iterating over the collection of elements we found earlier:
for (var i = 0, len = rsvpElems.length; i < len; ++i){
// storing the current element over which we're iterating:
tmp = rsvpElems[i];
// if i < n we're showing the rsvp element, otherwise we're hiding it:
tmp.style.display = i < n ? 'block' : 'none';
}
}
// getting all the inputs with type="radio" and name="number":
var radios = document.querySelectorAll('input[name="number"][type="radio"]');
// iterating over that collection:
for (var i = 0, len = radios.length; i < len; i++){
// binding an change event-handler to those radio elements
radios[i].addEventListener('change', showRSVP);
}
function showRSVP(e) {
// caching the changed-element (e is the event automagically passed
// to the function), e.target is the element upon which the event
// originated:
var checked = e.target,
n = parseInt(checked.value, 10),
rsvpElems = document.querySelectorAll('div[id^="rsvp"]'),
tmp;
for (var i = 0, len = rsvpElems.length; i < len; ++i){
tmp = rsvpElems[i];
tmp.style.display = i < n ? 'block' : 'none';
}
}
// getting a reference to the relevant 'label' element:
var label = document.querySelector('label[for="number"]');
// adding a change event-listener:
label.addEventListener('change', showRSVP);
|
Change part of href value
Date : March 29 2020, 07:55 AM
it helps some times You're missing the static part in your link building. In your Javascript, change $(".link").attr("href", links.join(","));
$(".link").attr("href", "static_part?variable=" + links.join(","));
|
Change button href to other href on page
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have a website with a logout link. , It can be something like this: jQuery(".mb-control").on("click", function() {
var box = $($(this).data("box"));
if (box.length > 0) {
box.toggleClass("open");
box.find('.btn-close').attr('href', this.href);
var sound = box.data("sound");
if (sound === 'alert')
playAudio('alert');
if (sound === 'fail')
playAudio('fail');
}
return false;
});
<a href="NEED TO PUT THE UNIQUE LOGOUT LINK HERE" class="btn btn-success btn-lg btn-close">Yes</a>
|
get the href value from selected option and change the href of the button
Date : March 29 2020, 07:55 AM
hope this fix your issue I want to get the href value of the selected option so that i can now go to the link that i selected using the button. , Add a id to => <a id="link" href=''><button type="button" class="btn btn-info">View</button></a>
$(document).ready(function() {
$("#childNames").on('change', function () {
var ref = document.getElementById("childNames").value;
alert(ref);
$('#link').attr('href', ref);
});
});
|
How can I create a button to change part of an href?
Date : March 29 2020, 07:55 AM
I hope this helps you . You have just to have your source url and then feed it with the input function changeURL(){
var source = "https://www.google.com/"
var input = document.getElementById("getData").value
document.getElementById("link").href = source + input
console.log(document.getElementById("link").href)
document.getElementById("getData").value = ""
}
<input id="getData" placeholder="email"/>
<br/>
<a id="link" href="https://www.google.com"> link </a>
<br/>
<button onclick="changeURL()">Click me </button>
|