How to check if an html tag with its unique identifier exists two or more times?
my pseudo code:
if($('#myId') > 1) { // exists twice }
The identifier selector only picks up the first element, which is the first on the page. Therefore, the length of the selector identifier must be 0or 1always.
0
1
So use the attribute instead of the selector and check its length.
if($('[id="myId"]').length > 1) { // exists twice }
if ($('[id="myId"]').length > 1) { console.log('twice'); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="myId"></div> <div id="myId"></div>
if($("[id=someId]").length > 1) { //Do Something }
or
if($("[id=someId]").size() > 1) { //Do Something }
if(document.querySelectorAll("[id=someId]").length > 1) { //Do Something }
,
if($("#" + name).length > 1) { // if exists twice }
exists = $("#" + name).length
if($("[id='myId']").length > 1) { // write your code here }
Source: https://habr.com/ru/post/1649528/More articles:Set a global variable only once in golang - gosparking - exception in the thread "submit-job-thread-pool-40" - streamingCaching images in ionic 2 typescript as picasso - cross-platformВставка данных в статический раздел Hive с использованием Spark SQL - hiveCannot find module './..../x.html' - TsLint / Angular -Metor - typescriptChange the color of android.support.v7.widget.SearchView - androidSpark 2.0.0: import SparkR CSV - csvSave Wikipedia text in English if German is not available with $ .getJSON in jQuery - javascriptApache Spark MLlib with the DataFrame API provides java.net.URISyntaxException when creating createDataFrame () or read (). Csv (...) - javaRtmp live stream using GStreamer - gstreamerAll Articles