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( ' ' ).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?
Try to execute it:
$('#abc\\@def.com').val(); First paragraph http://api.jquery.com/category/selectors/
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]" It seems that jQuery has a useful method for doing this since version 3, called escapeSelector .
$('#' + $.escapeSelector(my_id_with_special_chars));
$(document.getElementById('abc@def.com')) also works.
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(' ').load('{% url online_machines %}?id=' + idVal + '&bidvalue=' + bidValue); }); }); 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')); Give single quotes to both sides of the identifier, for example:
$('#abc@def.com').val()