1

I have a JSON file and i want to get its id by clicking on name

      [ 
        { ""      : 1
        , "id"    : 201610501
        , "name"  : "MainMenu"
        , "parent": null
        } 
      , 
        { ""      : 2
        , "id"    : "201610501*"
        , "name"  : "(*) Repeat"
        , "parent": 201610501
        } 
      ] 
and im fetching it like this
    fetch('https://uccx116p.ef.com:8445/3rdpartygadget/files/JSON_files/Retailer_UC_1.json')
            .then(res => res.json())
            .then(data => {
                console.log(data)
        
                document.querySelector("#app").innerHTML = data.map(user => {
                    return `<li id="list" onclick="openMessageBox(this)" ><a  href="#"> ${user.name}</a> 
      </li>
                    `
        
        
                })
            })
            .catch(() => console.log('cant'))
        }
    openMessageBox = function(el){
            var name =el.textContent
            console.log(name);

Right now on clicking the name im getting its name but I want to change it into its id how can I do that?

0

2 Answers 2

1

Try the below code

function openMessageBox(item){
alert(item);
}
function process() {
var data = [
    { "a": 1, id: 201610501, name: "MainMenu", parent: null },
    { "a": 2, id: "201610502", name: "(*) Repeat", parent: 201610501 }
  ];
  document.querySelector("#demo").innerHTML = data.map(user => {
                    return `<li id="list" onclick="openMessageBox(${user.id})" ><a  href="#"> ${user.name}</a> 
      </li>`;})
}

Check this

Sign up to request clarification or add additional context in comments.

10 Comments

Hey thanks alot but i want ids in the replacement of name on clicking i can use .replace method in javvascript but its returning all the ids i want the id of the specific name in the same object
@RufaidaShafiq Which selected item?
the id we are getting on alert i have store it in a variable now i have a cancel button and want to delete that selected id
@RufaidaShafiq clear that variable on cancel function.
openMessageBox = function(id){ updateCallVariable('callVariable1',id ) } its a function actually having parameters name and value i have given id as a value in it now how can i clear this in cancel function?
|
0

Let me clean your code a little bit using point-free:

const app = document.querySelector("#app");
   
const openMessageBox = (el, id, name) => { /* do stuff... */ }
   
const usersToListItems = userList => userList.map(user => 
    `<li class="list" onclick="() => {openMessageBox(this, ${user.id}, ${user.name});}" >
        <a  href="#">${user.name}</a>
     </li>`
).join('\n');
    
const toJson = res => res.json();
  
fetch('http:// ...')
    .then(toJson)
    .then(usersToListItems)
    .then(items => { app.innerHTML = items; })
    .catch(console.warn);

Haven't tested but I believe this could work.

The most important change to the logic was changing the onClick function to became an arrow function (preventing imediate call), that way you can pass parameters without invoking it. So I passed the element, id and name as parameters. Now you can use it as you like.

Changed your id inside li to a class because id must be unique.

The rest was just refactoring so you can have a easier to read code to deal with...

Now you can do something like el.querySelector('a').innerHtml = id; or something like that inside openMessageBox...

1 Comment

Thanks its not getting openMessageBox as an arrow function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.