0

I create button dynamically in my JS function and the put created button to the DOM.

Here is the code:

      var button = '<button id="btnStrView" type="button" onclick=' + parent.ExecuteCommand(item.cmdIndex) + ' class="button_air-medium">'+
                   +'<img id="streetView" class="miniToolbarContant" src="../stdicons/streetview-icon.png">'
                   +'</button>'

        $( "#tdStrView" ).append(button);

When I display the creted dynamically button in consle I see this:

"<button id="btnStrView" type="button" onclick=undefined class="button_air-medium">NaN</button>"

it seems that but not created properly the onclick is undefined and img tag is missing.

any idea what I do wrong? Why image button not created properly?

UPDATE:

I tryed to add double quotes to the onclick event:

onclick="' + parent.ExecuteCommand(item.cmdIndex) + '"

and the created button is:

"<button id="btnStrView" type="button" onclick="undefined" class="button_air-medium">NaN</button>"

the onclick is still undefined.

3
  • 1
    Don't create the button through HTML text. Use document.createElement('button') : developer.mozilla.org/en-US/docs/Web/API/Document/createElement Commented May 29, 2018 at 8:46
  • document.getElementById( "tdStrView").innerHTML=button; Commented May 29, 2018 at 8:50
  • I think problem is there: parent.ExecuteCommand(item.cmdIndex) please post full code to get the scenario. Commented May 29, 2018 at 9:05

3 Answers 3

1

You need to add double quotes.onclick will look like this onclick ="yourFunction()"

onclick="' + parent.ExecuteCommand(item.cmdIndex) + '"
Sign up to request clarification or add additional context in comments.

1 Comment

I tryed please see updaet.
0
// add double quotes in onClick and if you are getting NaN in place of image, 
// it means that it is trying to add numbers. I'm not sure yet why this is happening, but
// to fix that, add extra string in second line. like this. and then console button.

var button = '<button id="btnStrView" type="button" onClick="alert(7)" class="button_air-medium">'+
               +'' +'<img id="streetView" class="miniToolbarContant" src="../stdicons/streetview-icon.png">'
               +'</button>'

1 Comment

I believe your "item is a javascript variable". so it won't work in string. You should create button, image using document.createElement as suggested in above comments. It would be more easier.
0

While this code is a lot more verbose, it is more readable and less error prone.

const button = document.createElement('button');
button.id = 'btnStrView';
button.type = 'button';
button.className = 'button_air-medium';
button.addEventListener('click', event => {
  parent.ExecuteCommand(item.cmdIndex);
});

const img = document.createElement('img');
img.id = 'streetView';
img.className = 'miniToolbarContant';
img.src = '../stdicons/streetview-icon.png';

button.appenChild(img);
$( "#tdStrView" ).append(button);

Comments

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.