2

let's say I have a mark-up like this:

<script>
    var doSomething = function () {
        var container = $('.container');
        var list = $('#result-list');
        list.find('.result').attr('id', 'x');
        container.append(list.html());
    };
</script>

<script type="text/html" id="result-list">
    <div class="result"></div>
</script>

<a href="#" onclick="doSomething();">DO</a>
<div class="container">

</div>

I can't change div id attribute which is placed inside html-script. I don't know what or where is the problem. Any advice will be helpful.

13
  • 2
    Can script tag have a div element? Commented Feb 19, 2014 at 5:56
  • 1
    Yes, while you set its type to html-script. I use it for something like template in my markup Commented Feb 19, 2014 at 5:58
  • @hjpotter92 It can have whole HTML.. Commented Feb 19, 2014 at 5:58
  • 1
    @SaberAmani dont use . in your className.And what is issue..please rephrase your question Commented Feb 19, 2014 at 5:59
  • @Pilot well, how jquery knows it is an id or a class? Commented Feb 19, 2014 at 6:00

2 Answers 2

2

Are you looking for this.

var doSomething = function () {
    var container = $('.container');
    var list = $('#result-list');
    container.append(list.html());
     container.find('.result').attr('id','x');
};

Demo

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

3 Comments

but I can't understand what is different? Can you explain?
you are applying attribute on '.result' in script tag but in above code I apply attribute in container div after picking the .result from script tag
@RakeshKumar why can't one apply attribute on '.result' in script tag , where as '.html()' jquery function work's
1
<script>
    var doSomething = function () {
        var container = $('.container');
        var list = $('#result-list');
        var $result = $(list.html());
        $result.attr('id','x');
        container.append($result);
    };
</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.