1

Trying to get div 1 into div 2 and wrap brackets around the text.

Attempt

$('.div1').append('(' + $('.div2') + ')');

Before

<div class="1"></div>
<div class="2">blah</div>

What I want

<div class="1"><div class="2">(blah)</div></div>
2
  • You're referencing elements that have the classes div1 and div2 but your example is using elements with the class 1 and 2. Commented Nov 25, 2013 at 17:45
  • @Matt Coady you have 9 answers. None of them worked for you? Commented Nov 25, 2013 at 17:53

8 Answers 8

2
$('.div1').append($('.div2').html('(' + $('.div2').html() + ')'));

jsFiddle example

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

Comments

1

The problem is your selectors: .div1 and .div2, these are searching for elements that have the class of div1 and div2; whereas you want the div elements, with class 1 or 2.

Therefore I'd suggest:

$('div.2').text(function(i,t){
    return '(' + t + ')';
});
$('div.1').append($('div.2'));

JS Fiddle demo.

Although do be aware that using a numeral character as the first character of a class, or id, can be problematic in CSS.

References:

Comments

1

Try this

$(function(){
    $('div.2').text('('+$('div.2').text()+')');
    $('div.1').html($('div.2'));
});

Demo

Comments

0
$(function(){
    //$('.div1').append('(', $('.div2'), ')'); //this is better way but you can try following
    $('.div1').append($('.div2'));
    $('.div2').html('('+$('.div2').html()+')');
});

Demo: http://jsfiddle.net/aamir/jNU4B/

Comments

0

Try:

var html = $('.div2');
var text = $('.div2').text();
$(html).text("("+text+")");             
$('.div1').append(html);

Fiddle here.

Note:

Name of class never starts with numeric values.

Comments

0

You JS could look like this

$( document ).ready(function() {
  $('.1').append('(' + $('.2').html() + ')');
   $('.2').remove();
});

Here's a working example. http://jsfiddle.net/cVD6X/

Hopefully this is what you intended.

Regards, Eric Robinson

1 Comment

I know i put the Div tag within the ( and ) but hopefully that shouldn't make a difference.
0

Hope this helps..

$('div.1').append($('div.2'));
$('div.2').html("(" + $('div.2').html() + ")");

Comments

0

You should clone the element then add the parentesis to it, and append it to the other div. After that you can always remove the old element.

JS

var div2 = $('.div2').clone();
div2.text('(' + div2.text() + ')');
$('.div1').append(div2);

Html

<div class="div1"></div>
<div class="div2">blah</div>

Jsfiddle

1 Comment

This will give a result like this, <div class="1">(<div class="2">blah</div>)</div>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.