Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to call span element with replace?

Sorry about my noob question, but I just cant figure out how to do it in sugarcube:

I got 2 span elements and I want third one to copy contents of first one into second one:
<span id="n1">Text to copy</span>

<span id="n2"></span>

<span class=click>
<<click "copy goddamit">>
<<replace "#n2">>
????
<</replace>>
<</click>>
</span>

What should be in place of ???? to replace n2 with n1? I tried #n1, "#n1", and with different brackets but it doesn't work.

Comments

  • The problem is that the class attribute on the <span> is missing quotes:
    <span id="n1">Text to copy</span>
    
    <span id="n2"></span>
    
    <span class="click">
    <<click "copy goddamit">>
    <<replace "#n2">>
    ????
    <</replace>>
    <</click>>
    </span>
    

    You don't need the <span class="click"> for the above to work:
    <span id="n1">Text to copy</span>
    
    <span id="n2"></span>
    
    <<click "copy goddamit">>
    <<replace "#n2">>
    ????
    <</replace>>
    <</click>>
    

  • greyelf wrote: »
    The problem is that the class attribute on the <span> is missing quotes:
    <span id="n1">Text to copy</span>
    
    <span id="n2"></span>
    
    <span class="click">
    <<click "copy goddamit">>
    <<replace "#n2">>
    ????
    <</replace>>
    <</click>>
    </span>
    

    You don't need the <span class="click"> for the above to work:
    <span id="n1">Text to copy</span>
    
    <span id="n2"></span>
    
    <<click "copy goddamit">>
    <<replace "#n2">>
    ????
    <</replace>>
    <</click>>
    

    Well ok, but it just doesnt work anyway. You didnt answer the question: what should I write into replace for it to work?
  • First, why do you want to copy the contents of one <span> to another on the same page? Because, honestly, that seems like an odd thing to do. What are you trying to accomplish? Perhaps, there's a better/easier way.

    Second, there's no macro which will return the text from an element, so, at the moment, you can't without dipping into JavaScript. The requisite JavaScript is easy enough, if you really need to do something like that. For example:
    <span id="copy-source">Text to copy</span>
    
    <span id="copy-dest"></span>
    
    <<click "Copy">>
    <<script>>$("#copy-dest").html($("#copy-source").html())<</script>>
    <</click>>
    
    That will copy the contents of #copy-source to #copy-dest, including markup (which I assume you'd want).
  • First, why do you want to copy the contents of one <span> to another on the same page? Because, honestly, that seems like an odd thing to do. What are you trying to accomplish? Perhaps, there's a better/easier way.

    Second, there's no macro which will return the text from an element, so, at the moment, you can't without dipping into JavaScript. The requisite JavaScript is easy enough, if you really need to do something like that. For example:
    <span id="copy-source">Text to copy</span>
    
    <span id="copy-dest"></span>
    
    <<click "Copy">>
    <<script>>$("#copy-dest").html($("#copy-source").html())<</script>>
    <</click>>
    
    That will copy the contents of #copy-source to #copy-dest, including markup (which I assume you'd want).

    Oh yes thats awesome. Thanks a lot. Well, it basically comes from my previous question about creating many buttons on one page (twinery.org/forum/discussion/3066/execute-run-functions-after-a-press-on-the-same-page#latest), and I think this solves the problem. Thanks again!
  • Hmm. In that case. In the given code, the source content will replace any existing destination content each time. If that's the behavior you need, then carry on. If, on the other hand, you need the source content added to any existing content in the destination, then you'll want to change the code slightly. For example, the following will append to the source to the destination:
    <<click "Copy (Append)">>
    <<script>>$("#copy-dest").append($("#copy-source").html())<</script>>
    <</click>>
    
  • edited July 2015
    Hmm. In that case. In the given code, the source content will replace any existing destination content each time. If that's the behavior you need, then carry on. If, on the other hand, you need the source content added to any existing content in the destination, then you'll want to change the code slightly. For example, the following will append to the source to the destination:
    <<click "Copy (Append)">>
    <<script>>$("#copy-dest").append($("#copy-source").html())<</script>>
    <</click>>
    

    I actually need both, but I was ok doing the rest with <replace> macro.

    But since you proposed a variant, I have a question about it: your last example would just result in this:
    <span id="copy-dest"></span><span id="copy-source">Text to copy</span>
    

    or in this:
    <span id="copy-dest"><span id="copy-source">Text to copy</span></span>
    

    ?

    EDIT: well, I guess I can test it myself. Thanks for your help.
  • Neither. They copy the contents of the elements, since that is what you said you needed in the OP.

    Given the following setup:
    <span id="copy-source">Text to copy</span>
    
    <span id="copy-dest"></span>
    
    After one copy, you'd have this:
    <span id="copy-source">Text to copy</span>
    
    <span id="copy-dest">Text to copy</span>
    
    And with the appending version, after another copy, you'd have this:
    <span id="copy-source">Text to copy</span>
    
    <span id="copy-dest">Text to copyText to copy</span>
    
Sign In or Register to comment.