to get the text now it needs to get its...">

Special characters in html tag id

In the html code, I use the code <input type = "text" id = "abc@def.com"> to get the text now it needs to get its value, which is entered in the text field. I am using jquery for this:

 $( document ).ready( function() { $( ".bid" ).click( function() { idVal = this.id bidID = "#" + idVal + "bid" spanID = "#" + idVal + "cbid" bidValue = $(bidID).val(); alert(idVal) alert(bidID) alert(bidValue) //this alert says undefined $( spanID ).html( '&nbsp;' ).load( '{% url online_machines %}?id=' + idVal + '&bidvalue=' + bidValue); }); }); 

Having done this, I get an undefined value error. I tried to remove special characters from the identifier to get its value. But I need an identifier with special characters. How can I get its value using jquery?

+16
jquery html
source share
7 answers

Try to execute it:

 $('#abc\\@def.com').val(); 

First paragraph http://api.jquery.com/category/selectors/

+25
source share

Instead of trying to use an identifier with #, try this to match an attribute like this,

 $('[id=ID]') 

or

 $('[id='+ID+']') 

therefore instead

 bidID = "#" + idVal + "bid" 

you can try

 bidID = "[id=" + idVal + "bid]" 
+29
source share

It seems that jQuery has a useful method for doing this since version 3, called escapeSelector .

$('#' + $.escapeSelector(my_id_with_special_chars));

https://api.jquery.com/jQuery.escapeSelector/

+8
source share

$(document.getElementById('abc@def.com')) also works.

+3
source share

You have a chance at that, not 100% on how you do it. Let me know:

 $( document ).ready(function() { $(".bid").click(function() { idVal = $(this).attr('id'); bidID = "#" + idVal + "bid"; spanID = "#" + idVal + "cbid"; bidValue = bidID.val(); alert(idVal); alert(bidID); alert(bidValue); spanID.html('&nbsp;').load('{% url online_machines %}?id=' + idVal + '&bidvalue=' + bidValue); }); }); 
0
source share

Use the CSS.escape () function to select this DOM element. In your case, for the identifier abc@def.com you can use the code below.

 $("#" + CSS.escape('abc@def.com')); 
0
source share

Give single quotes to both sides of the identifier, for example:

 $('#abc@def.com').val() 
-4
source share

All Articles