레이블이 sourceCode인 게시물을 표시합니다. 모든 게시물 표시
레이블이 sourceCode인 게시물을 표시합니다. 모든 게시물 표시

2017년 12월 21일 목요일

html javascript radio handler 예제

html javascript radio handler 예제
2017-11-07

썩 좋은 예제는 아님...

전역 namespace 선언하고 
엘레먼트 구분하고 
사용할 엘레먼트 별 이벤트 만들어 넣고
핸들러 영역 구분하고
사용할 핸들러 만들어 넣음. 끝..
jquery 코드 부분은 DOM 기본 셀렉터로 바꾸는게 더 좋다고 생각하지만 귀찮으니까 이 정도로 정리..
멜론 티켓 때 코드를 안 옮겨 놔서 한 번 써봄...

toggle, on 등에 각각 shield 패턴이 들어가야 제대로 된 형태라고 볼 수 있을듯...
setProp 도 역시 jQuery 랩핑이라 그냥 그럼.... .prop 대신 쓸 수 있게 만들면 좋을듯..


var dom = {};
dom.ele = {};
dom.ele.radio = {};
dom.handler = {};
dom.handler.setProp = function(eleId,prop,value){
  $dom[eleId].prop(prop,value);
  //document.getElementById(eleId)[prop] = value;
};
dom.handler.toggle = function(eleId,prop){
    //var value = $dom[eleId].prop(prop) ? false : true;
    var value = document.getElementById(eleId)[prop] ? false : true;
    dom.handler.setProp(eleId,prop,value);
};
dom.ele.radio.toggle = function(eleId){
    var prop = 'checked';
    dom.handler.toggle(eleId,prop);
};
dom.ele.radio.on = function(eleId){
    var prop = 'checked';
    dom.handler.setProp(eleId,prop,true);
};
var $dom = {};
$dom.clazz = {};
var initDom = function() {
  $dom.btn_sms_y = $('#btn_sms_y');
  $dom.btn_sms_n = $('#btn_sms_n');
  $dom.btn_email_y = $('#btn_email_y');
  $dom.btn_email_n = $('#btn_email_n');
};
function init_agree(){
       
       var agreeText ="";
       if(smsBool){
              dom.ele.radio.on('btn_sms_y');
              //$('#btn_sms_y').prop( 'checked', true );
       }else{
              dom.ele.radio.on('btn_sms_n');
              //$('#btn_sms_n').prop( 'checked', true );
       }
       if(emailBool){
              dom.ele.radio.on('btn_email_y');
              //$('#btn_email_y').prop( 'checked', true );
       }else{
              dom.ele.radio.on('btn_email_n');
              //$('#btn_email_n').prop( 'checked', true );
       }
}

2016년 3월 29일 화요일

[스크랩]자바스크립트 중복 제거 소스코드

How do I check if an array has duplicate values?
If more than 1 element of the same exist, then return true. Otherwise, return false.
['hello','goodbye','hey'] //return false because no duplicates exist
['hello','goodbye','hello'] // return true because duplicaets exist
shareedit

marked as duplicate by Brian Roachmu is too shortJoseph SilberSasha ChedygovBen AlpertSep 11 '11 at 6:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
   
   
I don't want a list of duplicates removed. I just want to know true or false if a list has duplicates in it. – user847495 Sep 11 '11 at 6:08 
   
The accepted answer for the exact same question you asked is your answer.stackoverflow.com/questions/840781/… – Brian Roach Sep 11 '11 at 6:28 
   
1 
This question is not a duplicate. Since @user847495 simply wants to check if duplicates exists, the solution is faster/easier than what's needed to find all occurrences of duplicates. For example, you can do this:codr.io/v/bvzxhqm – alden Sep 26 '15 at 16:32 

3 Answers

up vote26down voteaccepted
If you have an ES2015 environment (as of this writing: io.js, IE11, Chrome, Firefox, WebKit nightly), then the following will work, and will be fast (viz. O(n)):
function hasDuplicates(array) {
    return (new Set(array)).size !== array.length;
}

If you only need string values in the array, the following will work:
function hasDuplicates(array) {
    var valuesSoFar = Object.create(null);
    for (var i = 0; i < array.length; ++i) {
        var value = array[i];
        if (value in valuesSoFar) {
            return true;
        }
        valuesSoFar[value] = true;
    }
    return false;
}
We use a "hash table" valuesSoFar whose keys are the values we've seen in the array so far. We do a lookup using Object.prototype.hasOwnProperty.call to see if that value has been spotted already; if so, we bail out of the loop and return true. (We don't use valuesSoFar.hasOwnProperty directly because that would break if the array contained "hasOwnProperty" as a string.)

If you need a function that works for more than just string values, the following will work, but isn't as performant; it's O(n2) instead of O(n).
function hasDuplicates(array) {
    var valuesSoFar = [];
    for (var i = 0; i < array.length; ++i) {
        var value = array[i];
        if (valuesSoFar.indexOf(value) !== -1) {
            return true;
        }
        valuesSoFar.push(value);
    }
    return false;
}
The difference is simply that we use an array instead of a hash table for valuesSoFar, since JavaScript "hash tables" (i.e. objects) only have string keys. This means we lose the O(1) lookup time of hasOwnProperty, instead getting an O(n) lookup time of indexOf.
shareedit

크롬 에서 번역 옵션 뜨는 거 막는 방법

버그 같은게 아니고 저 옵션의 기본 값이 제공으로 바뀐듯... 번역 옵션 제공을 비활성화하면 안 뜸. Chrome에서 웹페이지 번역 모르는 언어로 작성된 페이지를 방문할 때 다음 단계에 따라 Chrome이 페이지를 번역하도록 할 수 있습...