0

I'm pretty young and new to here so please bear with me. I am making a site and I am trying to create html links output in javascript. I am currently trying this method but it is not working? I am trying to change the 'mything' to show this text and add a link.

The code in javascript where I try to add a button is

document.getElementById('mything').innerHTML = place.name+' '+' <button id="edit" type = "button" class = "btn btn-link">(edit)</button>';

The bit for button is

   $('#mything').on('click', function() {
      $('#stuff2').hide(); 

   })

I hope I have put this in right? Thankyou

4
  • What exactly does not work? The click event? What should be the behavior? Should it redirect to the link's page? Or should there be a visible link instead of the button? Commented Apr 21, 2017 at 19:24
  • And your code adds a button and hides a (I assume) div, but nowhere do you actually create links via javascript. What are you trying, exactly? Commented Apr 21, 2017 at 19:26
  • The link is created in javascript and the button id is what triggers the event Commented Apr 21, 2017 at 19:39
  • What do you mean by link? In the context of JavaScript/HTML, a link generally refers to an (anchor tag](developer.mozilla.org/en-US/docs/Web/HTML/Element/a). Are you referring to buttons that trigger actions? Commented Apr 21, 2017 at 19:52

2 Answers 2

1

Using your code, this is what happens. I've commented the code up. Is this what you EXPECT to happen?

// Not sure what the place.name was coming from,
//   created a stub object to field that.
place = {
  name: "Foo"
};

// USe this to create the button, then create the
//   event listener ON the button.
// This uses the browser getElementById...
document.getElementById('mything').innerHTML = place.name+' '+' <button id="edit" type = "button" class = "btn btn-link">(edit)</button>';

// ...while the listener uses jquery to select the
//  element. On clicking, I toggle.
   $('#mything').on('click', function() {
      $('#stuff2').toggle(); 

   })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mything">

</div>
<div id="stuff2">
<p>Pellentesque in ipsum id orci porta dapibus. Curabitur aliquet quam id dui posuere blandit. Curabitur aliquet quam id dui posuere blandit. Nulla porttitor accumsan tincidunt.</p>

<p>Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Vivamus suscipit tortor eget felis porttitor volutpat. Nulla porttitor accumsan tincidunt. Vivamus suscipit tortor eget felis porttitor volutpat.</p>
</div>

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

Comments

1

Here's my attempt at illustrating what I think you're getting at:

var place = {
  name: 'Japan'
};
var span = document.getElementById('mything');
span.innerHTML =  place.name + ' <button id="edit" data-place="' + place.name + '">(edit)</button>';
span.querySelector('button').addEventListener('click', function(e){
  var place = this.getAttribute('data-place');
  alert(place);
});
<span id="mything"></span>

4 Comments

That's works but not what I need, I just want to create a link with a button id from javascript to be placed in a span
Everything you need to do that is in this example. Here, I'm inserting a button into a div, but it's the same process for inserting a button into a span.
Ok but can you show me an example just for one link please without the areas?
Updated. I'm still not sure if I understand what you're going for.

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.