JavaScript/jQuery

【jQuery/Selector #6】Attribute Ends With Selector [name$=”value”]

1Q74 2023. 2. 21. 15:37

1. Description

속성값의 끝나는 문자열과 매칭(대소문자 구분)되는 Element들을 찾는다.


2. Example

name속성이 story로 끝나는 요소들을 오른쪽으로 정렬한다.


[1q74.tistory.com] javascript/jquery/selector/attribute-ends-with

My Stories

  • 1. TStory
  • 2. Story Challenge
  • 3. UStory
  • 4. WStory
  • 5. Story Maker
  • 6. Love Story


  • [HTML Code]
    [//HTML Code]

    3. Code

    더보기
    <!-- ---------------------------------------------------------
      --
      -- Author: 1q74.tistory.com
      --
      --------------------------------------------------------- -->
    <!doctype html>
    <html lang="ko">
    <head>
    <meta charset="utf-8">
    <title>[1q74.tistory.com] javascript/jquery/selector/attribute-ends-with</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
    </head>
    <body>
    
    	<div name="_1q74-example-root">
    		<div name="_1q74-example-1">
    			<h4>My Stories</h4>
    			<li name="tstory">1. TStory</li>
    			<li name="story challenge">2. Story Challenge</li>
    			<li name="ustory">3. UStory</li>
    			<li name="wstory">4. WStory</li>
    			<li name="story maker">5. Story Maker</li>
    			<li name="love story">6. Love Story</li>
    		</div>
    
    		<div name="_1q74-example-bottom">
    			<br/>
    			<button name="btn-execute">#1. $("[name$='story']")</button>
    		</div>
    	</div>
    
    	<hr/>
    	<h5>[HTML Code]</h5>
    	<div name="_1q74-source">
    	</div>
    	<h5>[//HTML Code]</h5>
    
    <script>
    const btnExecute = $("[name='btn-execute']");
    const stories = $("[name$='story']");
    
    btnExecute.click(function() {
    	const styles = { "text-align": "right" };
    	const isStyled = (stories.css("text-align") === "right");
    	
    	if(!isStyled) {
    		stories.css(styles);
    	} else {
    		$.each(styles, function(key, value) {
    			stories.css(key, "");
    		});
    	}
    });
    
    // ------------------------------------------------------
    // 소스출력
    // ------------------------------------------------------
    var textSource = $("[name='_1q74-source']");
    var exampleHtml = $("[name='_1q74-example-root']").html();
    var specialCharLines = textSource.text(exampleHtml).html().split("\n");
    textSource.html(specialCharLines.join("<br/>").replaceAll("\t", "&nbsp;&nbsp;"));
    // ------------------------------------------------------
    </script>
    
    <style>
    [name|='_1q74-example'] li {
    	list-style-type: none;
    }
    
    [name|="_1q74-example"] button {
    	background: lightgray;
    	border: 5px outset;
    	min-height: 30px;
    }
    
    [name|="_1q74-example"] button:active {
    	border: 2px inset;
    	min-height: 30px;
    }
    </style>
    </body>
    </html>

    4. File

    [javascript][jquery][selector]attribute-ends-with.html
    0.00MB


    5. Reference

    https://api.jquery.com/attribute-ends-with-selector/