0

i have a sample code:

<span id="app_name_1"></span><a onclick="addSelectApplication('name 1');" href="#">Add name 1</a>                   
<span id="app_name_2"></span><a onclick="addSelectApplication('name 2');" href="#">Add name 2</a>               
<span id="app_name_3"></span><a onclick="addSelectApplication('name 3');" href="#">Add name 3</a>

And javascript

<script>
function addSelectApplication(title) {
    for(var i=0; i<3; i++) {
        parent.document.getElementById('app_name_'+i).innerHTML = title;
    }    
}
</script>

When I click Add name 1, result is

<span id="app_name_1">name 1</span> 
<span id="app_name_2">name 1</span>
<span id="app_name_3">name 1</span>

How to fix with result is:

When click Add name 1

<span id="app_name_1">name 1</span> 
<span id="app_name_2"></span>
<span id="app_name_3"></span>

When click Add name 2

<span id="app_name_1"></span>   
<span id="app_name_2">name 2</span>
<span id="app_name_3"></span>
2
  • Why you use parent? Is that inside an iframe or something Commented Nov 28, 2012 at 4:51
  • Did any of these answers address your issue? If so, please accept the one you feel helped the most. Commented Apr 25, 2013 at 17:31

4 Answers 4

2

You're looping over all of the spans and setting the title. You need to somehow determine which one you clicked, here from the title itself:

function addSelectApplication(title) {
    // Extract the link clicked from the title text
    var i = title.replace(/.*(\d)/, "$1");

    // Set the content
    parent.document.getElementById('app_name_'+i).innerHTML = title;
}

Cheers

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

Comments

0

Try this

 function addSelectApplication(title)
    {
        var splitStr = title.split(' ');

        for (var i = 1; i < 4; i++)
        {
            if (splitStr[1] == i.toString())
            {
                parent.document.getElementById('app_name_' + i).innerHTML = title;
            }
            else
            {
                parent.document.getElementById('app_name_' + i).innerHTML = "";
            }
        }
    }

Comments

0

For your current html format, the simplest way is:

    <span id="app_name_1"></span><a onclick="this.previousSibling.innerHTML = 'name 1';" href="#">Add name 1</a>               
    <br />
    <span id="app_name_2"></span><a onclick="this.previousSibling.innerHTML = 'name 2';"  href="#">Add name 2</a>               
    <br />
    <span id="app_name_3"></span><a onclick="this.previousSibling.innerHTML = 'name 3';"  href="#">Add name 3</a>

Comments

0
<span id="app_name_1"></span>   
<span id="app_name_2"></span>
<span id="app_name_3"></span>

<a title="name 1" onclick="addSelectApplication(this);" href="#">Add name 1</a>              
<a title="name 2" onclick="addSelectApplication(this);" href="#">Add name 2</a>             
<a title="name 3" onclick="addSelectApplication(this);" href="#">Add name 3</a>
<script>
    function addSelectApplication(obj) {     
    for(var i=1; i<=3; i++) {        
        var no = obj.title.split(' ');
        if (no[1] == i) {
            document.getElementById('app_name_'+i).innerHTML = obj.title;     
        } else {
            document.getElementById('app_name_'+i).innerHTML = '';     
        }
    }   
    return false;    
}
</script>

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.