0
<tr>
<td>Name:</td>
<td><span id= "keep">Username</span><input id= "change" value= "Name" style= "display:none;"></td>  
</tr>

I have this, what kind of javascript do I need in order to hide the span and let the input show? This should be done by clicking this edit button:

<a href="#" data-original-title="Edit this user" data-toggle="tooltip" type="button" class="btn btn-sm btn-warning"><i class="glyphicon glyphicon-edit"></i></a>

I have checked other questions on this site for duplication, but I tried alot and none answered my question!

7
  • 1
    The input is inside the span. A man can literally not hide that span and showing the input at the same time. Commented Dec 3, 2016 at 16:03
  • I never knew that there is a type="button" attribute available to anchor tag Commented Dec 3, 2016 at 16:03
  • Ok if I put it outside how could I make it view later? @Lain Commented Dec 3, 2016 at 16:03
  • @hiba: document.querySelector('#change').style.display = ''; Commented Dec 3, 2016 at 16:04
  • Yes exactly. You cannot hide the span, as it will hide the input also. I would recommend reconsidering modifying the HTML. Otherwise, you can use remove and append javascript functionality to achieve your requirement Commented Dec 3, 2016 at 16:06

2 Answers 2

1
<html>
    <head>
        <script>
            function showMe(){
                document.querySelector('#change').style.display = ''; //Show the input
                document.querySelector('#keep').style.display = 'none' //Hide the span
            }
        </script>
    </head>

    <body>
        <tr>
            <td>Name:</td>
            <td>
                <span id = 'keep'>Username</span>
                <input id = 'change' value= 'Name' style= 'display:none;'>
            </td>  
        </tr>
        <a href = '#' data-original-title = 'Edit this user' data-toggle = 'tooltip' type = 'button' class='btn btn-sm btn-warning' onclick = 'showMe()'><i class = 'glyphicon glyphicon-edit'>click me</i></a>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way I could save what the edit is afterward the edit is done? For example if I refresh the page, the edit stays as they edited it?
<a data-original-title="Remove this user" data-toggle="tooltip" type="button" class="btn btn-sm btn-success"><i class="glyphicon glyphicon-saved"></i></a> Like if I click this save button it saves the edits?
Ehm yes, with localStorage, yet a man should create another question for that.
1

Use This Simple Code

first add library

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

your html code

<a href="#" data-original-title="Edit this user" data-toggle="tooltip" type="button" class="btn btn-sm btn-warning"><i class="glyphicon glyphicon-edit"></i>edit</a>

<span id= "keep">Username</span><input id= "change" value= "Name" style= "display:none;"></span>

and use script

<script>
$(document).ready(function(){
    $("a").click(function(){
        $(" #change").toggle();
         $("#keep").hide();
    });
});
</script>

1 Comment

Reclicking will give weird behavior with one toggle and one static hide.

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.