getElementsByTagName , anchor tag, adding it to pargraph innerHtml , Show out the href attribute value only !!! any explanation?

when I saw this example , it used getElementsByTagName() method to bring all anchor tags then using the index [0] to bring the first element inside this HTMLCollection. assign it to x variable. if I console.log(x) I will get what is expected the first anchor tag in my HTML DOM.
but if I assign this X to a paragraph using innerHTML , the result will show the href value which inside the anchor , I expected to add the whole anchor tag as HTML element and show inside the paragraph the content of it which is in this example ‘Hello world’.
any explanation why this is happening? Thank you.

const x = document.getElementsByTagName('a')[0];
console.log(x);
// will print out the anchor tag element
document.getElementById('demo').innerHTML = x;
// will add the href attribute value of the anchor only, But I expected that I will get the text 'hello world'
<!DOCTYPE html>
<html>

<body>
  <a href="https://www.w3schools.com/html/">hello world</a>
  <a href="https://www.w3schools.com/css/">Welcome</a>
  <p id="demo"></p>

</body>

</html>

I am reading an Excel spreadsheet with SheetJS & JS-XLSX but I’m losing the subheadings, Is there a way to keep subheadings?

I recently started working with SheetJS and JS-XLSX and when I read in the file, I am losing some of the sub-headings as a key value.

The file looks like this:

Excel File Example

My line of code that brings in the data is:

var data = xlsx.utils.sheet_to_json(workbook.Sheets[sheetName], {range:1});

I’m using range because the first row is instructions, not headings. The object that’s returned will have the job titles as the key for the values (for salaries only) and the bonus column has __empty_1: , __empty_2: , etc.

What I need to have the object return is JobTitle.Salary.amount and JobTitle.Bonus.amount

Is there a way to do this with SheetJs or is there a better tool to receive and parse the data?

Thanks for any help!

Javascript – Show / Hide password function for multiple input fields

I have a small function which show and hide passwords in the input fields. It works fine when applied to a single input field, but when I apply to multiple fields it behaves wrong.

For example: by clicking on the input field new password, the function is activated for all the other fields, instead it should be activated only for the desired field.

How can I correct this unwanted behavior? Sorry but I’m relatively new, I appreciate any response, thanks.

function showPassword() {
  var x = document.getElementById("password_current");
  if (x.type === "password") {
    x.type = "text";
  } else {
    x.type = "password";
  }

  var y = document.getElementById("password_1");
  if (y.type === "password") {
    y.type = "text";
  } else {
    y.type = "password";
  }


  var z = document.getElementById("password_2");
  if (z.type === "password") {
    z.type = "text";
  } else {
    z.type = "password";
  }
}
label.t2 {
    font-size: 14px!important;
  line-height: 1.5em!important;
  font-weight: 500!important;
    margin-bottom: 6px!important;
    display: block;
}

/*Toggle Password class*/
#togglePw { display: none; }
#togglePw + label:before { content: "f06e"; }
#togglePw:checked + label:before { content: "f070"; }
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">

<p class="">
    <label class="t2" for="password_current">Current Password</label>
    <input type="password" class="field-settings" name="password" id="password_current" autocomplete="off"/>
    <input type="checkbox" id="togglePw" onclick="showPassword()"/>
    <label for="togglePw" class="fa"></label>
</p>

<p class="">
    <label class="t2" for="password_1">New Password</label>
    <input type="password" class="field-settings" name="password_1" id="password_1" autocomplete="off" />
    <input type="checkbox" id="togglePw" onclick="showPassword()"/>
    <label for="togglePw" class="fa"></label>
</p>

<p class="">
    <label class="t2" for="password_2">Repeat New Password</label>
    <input type="password" class="field-settings" name="password_2" id="password_2" autocomplete="off" />
    <input type="checkbox" id="togglePw" onclick="showPassword()"/>
    <label for="togglePw" class="fa"></label>
</p>

Google Chrome extension margin/blank space insert for image

I’m designing a Google Chrome extension. It is designed to insert an image into text. It currently does so, but the image attached at the bottom shows my problem. The image displays over text, and I was wondering if there was a way to make the image have its own separate space, as if I was using margin – so the snake is to the left of the logo and all of the links, and everything else on the page is shifted to the right slightly. Neither marginLeft nor marginRight work, though. This is my code:

popup.js:

  function imageappend() {
    const img = document.createElement("img");
    img.src = chrome.runtime.getURL("imageedit_1_3932659157.png");
    img.style.position = "fixed";
    img.style.top = "0";
    img.style.left = "0";
    img.style.padding = "0";
    img.style.marginRight = "7%";
    img.style.marginLeft = "0";
    img.style.marginBottom = "0";
    img.style.marginTop = "0";
    img.style.width = "5%";
    document.body.appendChild(img);
  }

  let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

  chrome.scripting.executeScript({
    target: { tabId: tab.id },
    function: imageappend
  });
})();

Image spawns over text

Regex validating string of numbers

I am trying create regex witch will start with some number like 230, 420, 7456. Then could be some number in interval 1-9 but the final length must be 9.

For example 230888333 or 745623777

I create this:

([(230|420|7456)[0-9]{1,}]{9})

But it is not correct. Any idea how to make this correct?

Thaks.

tsc requires via package name instead of relative path when emitting declarations

I forked a JavaScript package for another (TypeScript) project as I had to change some things and I wanted it to emit declaration files.

When running tsc locally, everything compiles and the require statements look fine while using relative paths for local files (as desired):

index.d.ts (1):

import Connection = require("./lib/connection");
import Client = require("./lib/client");
import Discovery = require("./lib/discovery");
export { Connection, Client, Discovery, Discovery as discovery };

But when installing it in another project via npm, tsc is invoked during postinstall phase and the generated declarations use require statements with the package name followed by the past instead (this is of course invalid and no typings are gathered by my IDE):

index.d.ts (2):

import Connection = require("@ercksen/esphome-native-api/src/lib/connection");
import Client = require("@ercksen/esphome-native-api/src/lib/client");
import Discovery = require("@ercksen/esphome-native-api/src/lib/discovery");
export { Connection, Client, Discovery, Discovery as discovery };

I found out that running npm run postinstall locally also produces correct results. And manual compilation inside the node_modules/@ercksen/esphome-native-api folder of the referred project also creates faulty declarations. So it has nothing to do with the fact that I create the declarations when installing (instead of pre-built).

The interesting part is: .js files work fine! How can I get that working also for .d.ts files? I think this is not the intended behavior.

Steps to reproduce (you will probably run into issues with my SSL certificate, but that would be a topic for another question):

Following works. See dist/index.d.ts (1)

git clone https://git.ercksen.de/home-automation/esphome-native-api.git
npm i
tsc

Following does not work. See node_modules/@ercksen/esphome-native-api/index.d.ts (2)

npm i git+https://git.ercksen.de/home-automation/esphome-native-api.git

Any help would be greatly appreciated!

How to hide all other div except one that is clicked?

I have this div:

<div id="campingTrips"><h1>Camping trips</h1>
</div>

<div id="cityBreaks"><h1>City breaks</h1>
</div>

<div id="roadTrips"><h1>Road trips</h1>
</div>

<div id="cruises"><h1>Cruises</h1>
</div>

<div id="groupTours"><h1>Group tours</h1>
</div>

<div id="girlsTrips"><h1>Girls’ trips</h1>
</div>

How to hide other and left just one that is click?

I tried something like this

$(document).ready(function() {

$(‘.campingTrips’).click(function(e) {
var image = e.target,
interval,
height = 200,
width = 200,
z = $(this).css(“z-index”);
$(this).css(“z-index”, z + 10);
$(‘#product-container’)???.addClass(‘disable-click’);
$(‘.unhidden’).not($(this).parent().parent()).addClass(‘noOpacity’);
});
});
But this doesnt work since all my divs has different ids

dynamically adding a button in html page with simple Javascript without any framework or library

hey there i’m making a simple webpage which requires to download an output image file at the last step..
but i don’t know how can i add download button dynamically at correct time, because at starting of a page there is no need of download button..

so i have main.js file:
which looks something looks like this:

let img_code=document.getElementById('img_code');
let textbox=document.getElementById('textbox');
let gen_button_img=document.getElementById("img_button");

gen_button_qr.addEventListener("click",()=>
{
 var trailer=textbox.value;
 var url='www.example.com';
 var result= url.concat(trailer);
if (navigator.onLine)
{
    if(trailer.length<=1725 && trailer.length>0)
    {
        if((trailer !="0")&& (trailer.replace(/s/g, '').length))
        {
            image_code.src=result;
            alert("Image Generated successfully");
      
            
            /**/
        }
        else
        {
            alert("You cannot create this file spaces only or only with single 0");
        }
    
    }
    else
    {
        alert("Maximum charecter limit is exceeded!! ");
    }
}
else
{
    alert("No Internet Connection");
}

});

So, i have the question is there any way to dynamically add the button which takes file URL as input and download that file through web browser’s downloader?

Note=>
I can easily save the result by right click on the picture and save image option; but i want to add an extra button to download the same file.

Prevent soft keyboard dismissing on text input blur

I am building a Web chat application.

I have a chat input, which I would like to keep focus on even when the “send” button is clicked. So I want the keyboard to stay open even when a chat is sent, either by pressing a “send” button, or by clicking on the “send” in the mobile’s keyboard itself. This is the behavior you get in messaging apps like Whatsapp, iMessage, etc.

Change total_fields limit for Elasticsearch index using node.js client

I’m using node.js Elasticsearch 7.17 client to create default index settings, and I’d like the total_fields limit to be more than the default 1000. When I try the following:

export const defaultCaseIndexSettings: IndicesIndexSettings = {
  'index.number_of_replicas': 0,
  'index.number_of_shards': 6,
  'index.max_result_window': Number(INDEX_MAX_RESULT_WINDOW),
  'index.mapping.total_fields.limit': 10000,
etc.
}

it seems that index.mapping.total_fields.limit is not a setting that can be set on IndicesIndexSettings. I’ve also tried:

export const defaultCaseSettingsIndexConfig: IndicesCreateRequest['body'] = {
  settings: {
    index: {
      number_of_shards: '2',
      number_of_replicas: '2',
      mapping: {
        total_fields: {
          limit: 10000,
        },
      },
    },
  },

and that gives me the message that Object literal may only specify known properties, and 'mapping' does not exist in type 'IndicesIndexSettings'. How can I set the total_fields limit using nodejs Elasticsearch client?

Is there any way I can update a mounted React node’s props in my current setup?

So I have a mounted React application that is rendered like this (it’s via another framework on top):

const componentRender = (entryComponent, initialProps, targetNode) => {
 const root = createRoot(targetNode);
 const element = createElement(entryComponent, initialProps);
  const wrapper = createElement(SomeWrapper, null, element);
  root.render(wrapper);
}

On top level, I call:

Ext.extend(Ext.Container, {
  listeners: {
    afterrender: function () {
     this.root = componentRender(
        MyComponent,
        this.myProps,
        domElement
      );
    },
  },
 constructor: function (...args) {
    Object.assign(this, args);
  },
onServerSuccess: function(data) {
// here is where I receive new data 
    this.myProps = data;
}
});

In essence, when the EXT component loads, I insert React into the page. So what I can do is onServerSuccess (when I receive the data I want), I can use this.root.unmount() and mount the component again. However, mounting and unmounting repeatedly is not really the best solution. What I’m trying to do is perhaps to make this.myProps “reactive” so whenever they change, the component knows and re-renders without having to be unmounted and re-mounted again.

Maybe there’s a better solution out there?

How to program calculator with typescript

I have to program a calculator. there have to be 2 inputfields for number1&2. And when you click for example “+” the numbers in the inputfields have to be added to each other an the result has to be shown in input id=”result”.
Can anyone help me with the typescript code, because I honestly have no idea how to even start…

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="node_modules"></script>
    <script src="script.js"></script>
    <link rel="stylesheet" href="style.css">

</head>
<body>
<table>
    <tr>
        <td> <input id="number1" type="number" placeholder="Zahl1"></td>
        <td> <input id="number2" type="number" placeholder="Zahl2"></td>
        <td> <input id="resultArea" type="number" placeholder="Lösung" disabled></td>
    </tr>
    <tr>
        <td> <button  onclick="calculateResult()" id="plus">+</button></td>
        <td> <button  onclick="calculateResult()" id="minus">-</button></td>
        <td> <button  onclick="calculateResult()" id="multiply">*</button></td>
        <td> <button  onclick="calculateResult()" id="divide">/</button></td>
    </tr>
</table>
</table>
</body>
</html>

Trouble importing json file to .js file

I have a simple javascript file that’s meant to fetch an object as a json file and display it in the console:

javascript code

When I run the page on my app, I get these error messages:

errors

As far as I can tell, my directories are set up fine and I don’t think there are any typos in the code. I think the problem has something to do with not being able to find the json file. I’ve even tried putting the json file in the same folder as main.js but this hasn’t worked either. This is how my directory is laid out anyway in case the error has something to do with that:

Directories

I’m making an app using Flask and ultimately want to be able to store the json object in a js variable so I can use d3 to make tree graphs with it.