මේ Code එකේ අවුල මොකක්ද HTML, JS, CSS

ryangate2

Well-known member
  • Jan 11, 2022
    506
    654
    93
    The issue with your code is the use of `!important` within JavaScript to change CSS styles. The `!important` rule is a part of CSS and it's not supposed to be used with JavaScript in this way. It has no effect in JavaScript and can cause your code not to work as expected.

    Here's the correct code:

    ```javascript
    const selectElementOrc1 = document.getElementById('meta_orcapplicable');
    const displayElementOrc1 = document.getElementById('new_orcincluded');

    selectElementOrc1.onchange = function() {
    if (selectElementOrc1.value === 'Applicable') {
    displayElementOrc1.style.display = 'block';
    } else {
    displayElementOrc1.style.display = 'none';
    }
    };
    ```

    This will show and hide your `displayElementOrc1` based on the value of `selectElementOrc1`. However, if you have some CSS rules that are using `!important` to set the display of `displayElementOrc1` and you still want to override them from JavaScript, you will need to use CSS classes instead.

    For instance:

    ```css
    /* Add this in your CSS */
    .display-block-important {
    display: block !important;
    }

    .display-none-important {
    display: none !important;
    }
    ```

    And your JavaScript will be:

    ```javascript
    const selectElementOrc1 = document.getElementById('meta_orcapplicable');
    const displayElementOrc1 = document.getElementById('new_orcincluded');

    selectElementOrc1.onchange = function() {
    if (selectElementOrc1.value === 'Applicable') {
    displayElementOrc1.classList.remove('display-none-important');
    displayElementOrc1.classList.add('display-block-important');
    } else {
    displayElementOrc1.classList.remove('display-block-important');
    displayElementOrc1.classList.add('display-none-important');
    }
    };
    ```

    This will effectively override any existing CSS rules with the `!important` declaration.
     

    hasithayad

    Well-known member
  • Sep 28, 2011
    30,793
    1
    45,000
    113
    css, js දෙකේම !important කෑලි දෙක අයින් කරන්න. වැඩ කරනවා
     

    geeko

    Well-known member
  • Mar 18, 2013
    7,472
    4,471
    113
    Default value rkak deelafa balanna dropdown ekata.

    Nathnm display wenuwata visibility propery use krnna balanna.
    ------ Post added on Jun 24, 2023 at 2:23 AM
    onchange ekene aula thiyenne display ekata enakal code eka run venne na. a nisa display style part eke aulak neme

    me fiddle eke issue eka enne na neda
    css eken important ain kaloth enne na. eth real code ekedi css eke important ain karanna ba. ethakota page eka load veddi display vela thiyenne hide venna one fields tika
    ------ Post added on Jun 25, 2023 at 12:42 AM

    Matath methana aul wage🤨

    Onload function ekata daala balanna js code eka

    https://www.w3schools.com/jsref/event_onload.asp
    baluwa hariyanne na
    ------ Post added on Jun 25, 2023 at 12:43 AM

    null ekaka onchage set kranna ba kiyala ne kiyanne

    meke null eka wenne selectElementOrc1 variable eka neda

    eka null wenne element eka hariyta selct wenne nathi nisa.

    element eka hariyta select wenne naththe ekko wrong id, or element eka thama dom eke na
    meka plugin ekak vidihata hapu ekak machan. hook ekakin form ekakata sambanda karala thiyenne me fields tika venama thiyenne. ID eke hari deveniyata kiyapu eka venna widihakuth na man footer eke yatatama dalath baluwa script eka eth hariyanne na. Same error.
    ------ Post added on Jun 25, 2023 at 12:46 AM

    Put below after your </form> tag:
    meka plugin ekak vidihata hapu ekak machan. hook ekakin form ekakata sambanda karala thiyenne me fields tika venama thiyenne. ID eke hari deveniyata kiyapu eka venna widihakuth na man footer eke yatatama dalath baluwa script eka eth hariyanne na. Same error
    ------ Post added on Jun 25, 2023 at 12:47 AM

    this works ✅
    external script file ekak nam onnam udin dala defer daanna..
    meka plugin ekak vidihata hapu ekak machan. hook ekakin form ekakata sambanda karala thiyenne me fields tika venama thiyenne. ID eke hari deveniyata kiyapu eka venna widihakuth na man footer eke yatatama dalath baluwa script eka eth hariyanne na. Same error
    ------ Post added on Jun 25, 2023 at 12:49 AM

    The issue with your code is the use of `!important` within JavaScript to change CSS styles. The `!important` rule is a part of CSS and it's not supposed to be used with JavaScript in this way. It has no effect in JavaScript and can cause your code not to work as expected.

    Here's the correct code:

    ```javascript
    const selectElementOrc1 = document.getElementById('meta_orcapplicable');
    const displayElementOrc1 = document.getElementById('new_orcincluded');

    selectElementOrc1.onchange = function() {
    if (selectElementOrc1.value === 'Applicable') {
    displayElementOrc1.style.display = 'block';
    } else {
    displayElementOrc1.style.display = 'none';
    }
    };
    ```

    This will show and hide your `displayElementOrc1` based on the value of `selectElementOrc1`. However, if you have some CSS rules that are using `!important` to set the display of `displayElementOrc1` and you still want to override them from JavaScript, you will need to use CSS classes instead.

    For instance:

    ```css
    /* Add this in your CSS */
    .display-block-important {
    display: block !important;
    }

    .display-none-important {
    display: none !important;
    }
    ```

    And your JavaScript will be:

    ```javascript
    const selectElementOrc1 = document.getElementById('meta_orcapplicable');
    const displayElementOrc1 = document.getElementById('new_orcincluded');

    selectElementOrc1.onchange = function() {
    if (selectElementOrc1.value === 'Applicable') {
    displayElementOrc1.classList.remove('display-none-important');
    displayElementOrc1.classList.add('display-block-important');
    } else {
    displayElementOrc1.classList.remove('display-block-important');
    displayElementOrc1.classList.add('display-none-important');
    }
    };
    ```

    This will effectively override any existing CSS rules with the `!important` declaration.
    THIS WORKS! THIS WORKS! THIS WORKS! THIS WORKS! THIS WORKS! THIS WORKS! THIS WORKS! THIS WORKS!

    THANK YOU BRO!
    🙏

    ------ Post added on Jun 25, 2023 at 12:58 AM
     

    geeko

    Well-known member
  • Mar 18, 2013
    7,472
    4,471
    113
    css, js දෙකේම !important කෑලි දෙක අයින් කරන්න. වැඩ කරනවා
    CSS eke important ain kaloth Applicable select kalath nathath display vela thiyenawa. ප්‍රශ්නේ විසඳුනා මචන් උඩ Reply එකේ තිබ්බ විදිහට කලාම.

    ප්‍රශ්නේ විසඳුනා .උදව් කරපු හැමෝටම THANKS. @ryangate2 THANKS
    ------ Post added on Jun 25, 2023 at 1:04 AM
     

    හෙනයා

    Well-known member
  • May 23, 2014
    16,872
    17,084
    113
    Kottawa
    meka plugin ekak vidihata hapu ekak machan. hook ekakin form ekakata sambanda karala thiyenne me fields tika venama thiyenne. ID eke hari deveniyata kiyapu eka venna widihakuth na man footer eke yatatama dalath baluwa script eka eth hariyanne na. Same error.

    එහෙම මගුලක් උබ කලින් කිය්ල නෑනේ.. නිකං ෆිඩල් එකක් දාල වැඩ නැත්තෙ ඇයි කියල අහනවා.. දාද්දි කන්ටෙක්ස්ට් එකත් එක්ක දාපං මුලින්..

    THIS WORKS! THIS WORKS! THIS WORKS! THIS WORKS! THIS WORKS! THIS WORKS! THIS WORKS! THIS WORKS!

    THANK YOU BRO!
    🙏

    👍
     

    geeko

    Well-known member
  • Mar 18, 2013
    7,472
    4,471
    113
    එහෙම මගුලක් උබ කලින් කිය්ල නෑනේ.. නිකං ෆිඩල් එකක් දාල වැඩ නැත්තෙ ඇයි කියල අහනවා.. දාද්දි කන්ටෙක්ස්ට් එකත් එක්ක දාපං මුලින්..



    👍
    එහෙම ෆයිල්ස් දෙකක වැඩේ වෙන නිසා තිබ්බ අවුලක් නෙමේ කියලා මට තේරුණා. මන් දාලා තිබ්බ කොටස් ටිකේ තමයි අවුල තියෙන්න ඕනේ කියලා හිතුනේ. ඒකයි ඒ ටික විතරක් දාලා ඇහුවේ. ඇත්තටම අවුල තිබ්බේ දාලා තිබ්බ කෝඩ් එකේනේ
     
    • Like
    Reactions: හෙනයා