0

I am new to jquery. I designed form field input text with span text.

<div class="input-group" style="width:40%">
    <input type="text" name="emial" id= "emial" class="form-control" required="true"/>
    <span class="input-group-addon">@gmail.com</span>
</div>

i am trying get the value in jquery

var emial=$("#emial").val();

but i am getting only input text not with @gamil.com

5
  • @Pramod Patil: it's not duplicate please check care fully and read my question. Commented Apr 4, 2017 at 6:39
  • 2
    Possible duplicate of How do I get a value of a <span> using jQuery? Commented Apr 4, 2017 at 6:51
  • @Amr Aly: it's not duplicate please check care fully and read my question Commented Apr 4, 2017 at 7:12
  • Nothing personal but what i really see that you are trying to get the text of span am i right? Commented Apr 4, 2017 at 7:37
  • Not only span,text box value with span.Combination here. Commented Apr 4, 2017 at 9:21

5 Answers 5

3

You need to get the text content of span immediately after the element. Where use next() method to get the span element and use text() method to get the text content.

var $email = $("#emial");
var email = $email.val() + $email.next().text();

$('#email').on('input', function() {
  var $email = $(this);
  var email = $email.val() + $email.next().text();
  console.log(email);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input-group" style="width:40%">
  <input type="text" name="email" id="email" class="form-control" required="true" />
  <span class="input-group-addon">@gmail.com</span>
</div>

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

Comments

1

Span is not input tag . val() is used for input type tags only.

use it like:

var emial = $("#emial").val() + $("#emial").next("span").text(); 

Comments

0
$( "#emial" ).html();

you will get the full result with html content

Comments

0

You need to use .text or .html method for span and div

var email=$('.input-group-addon').text();

or

var email=$('.input-group-addon').html();

In place of .input-group-addon give Id to the span call the same function

Comments

0

To get the value of a span element, use either .text() or .html()

var emial =$("#emial").val() + $("#emial").next("span").text();

var emial=$("#emial").val() + $("#emial").next("span").text();
console.log(emial)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input-group" style="width:40%">
    <input type="text" name="emial" value="mail" id="emial" class="form-control" required="true"/>
    <span class="input-group-addon">@gmail.com</span>
</div>

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.