Last active
June 6, 2018 12:29
-
-
Save tarjei/2679959dbfb102862e15b02a09e29d6e to your computer and use it in GitHub Desktop.
Problem getting radio group to fire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="form-gdpr"> | |
<div class="form-gdpr-title">Ønsker du en gang i blant å bli kontaktet om spennende nye tilbud og tjenester?</div> | |
<div class="form-group check-group"> | |
<label class="ip-label-check form-gdpr-ok" for="form-marketing"> | |
<input type="radio" | |
value='1' | |
bind:group=userData.marketing | |
name="marketing" | |
class="ip-check" | |
id="form-marketing"/> | |
<span class="display-check"> </span> | |
Ja dere kan ta kontakt på epost og SMS. | |
</label> | |
</div> | |
<div class="form-group check-group"> | |
<label class="ip-label-check" for="form-marketing-no"> | |
<input type="radio" | |
value='0' | |
bind:group=userData.marketing | |
name="marketing" | |
class="ip-check" | |
id="form-marketing-no"/> | |
<span class="display-check"> </span> | |
Nei, det vil jeg ikke. | |
</label> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setField(selector, value) { | |
var element = document.querySelector(selector) | |
if (!element) { | |
throw new Error( | |
'setField requires a real element! Not found for: ' + selector | |
) | |
} | |
var evt = document.createEvent('HTMLEvents') | |
if (element.type === 'radio') { | |
//element.checked = true | |
// moi importante! Fire an input! | |
//evt.initEvent('change', false, true) | |
evt.initEvent('change', false, true) | |
console.log('FIREW!, ', value, selector, element.value, element.checked) | |
} else if (element.type === 'checkbox') { | |
element.checked = value | |
// moi importante! Fire an input! | |
evt.initEvent('change', false, true) | |
} else { | |
element.value = value | |
// moi importante! Fire an input! | |
evt.initEvent('input', false, true) | |
} | |
element.dispatchEvent(evt) | |
} | |
// test: | |
setField('#form-marketing', '1') | |
In the component: | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment