I have a string which looks like this: (id:561644cdb40fbe0100247dd7:q) (id:56165d8a79c8c40100adbdb6:q) and I need to replace the different id's with different values. I already have the id's in a variable and trying to loop through with something like this var mailId = "(id:" + rplcId + ":q)"; But If I use the replace() function it doesnt work...Any other suggestions?
You can select the id with:
"(id:56165d8a79c8c40100adbdb6:q)".split(":")[1]
var id = "(id:561644cdb40fbe0100247dd7:q)";
var idArr = id.split(":");
idArr[1] = newId; //56165d8a79c8c40100adbdb6
var mailId = idArr[0]+idArr[1]+idArr[2];
and please provide your full code
Related
I'm trying to use a variable that contains the name of a HTML element in a JQuery function for a radio button.
Normally I would do something like this:
var oldRadioValue = $('input[name="correct10")"]:checked').val();
But due to the radio button being created dynamically I need to do something like this:
var radioName = "correct"+questionCount;
var oldRadioValue = $('input[name=radioName]:checked').val();
When I check the console.log() I am getting undefined...
Thanks in advance for any help!
You should be able to it like this do this
var oldRadioValue = $('input[name="' + radioName + '"]:checked').val();
You can either use concatenation like this:
var oldRadioValue = $('input[name="'+radioName+'"]:checked').val()
Or the newer template literal syntax:
var oldRadioValue = $(`input[name="${radioName}"]:checked`).val();
Your need string concatenation to make this work.
var oldRadioValue = $('input[name=' + radioName + ']:checked').val();
Is there a better way to write jQuery('#'+ myId) in JavaScript.
I have an ID in a variable without the # character and jQuery('#'+ myId) looks ugly.
var myId = 'last-element-id';
jQuery('#'+ myId)
I'd like to avoid the + character to join the strings.
Thanks
But, have in mind that returned element is not a jQuery collection
function getElement(element) {
return document.getElementById(element)
}
function jQueryID(myId){
return jQuery('#'+ myId);
}
and then call like
jQueryId(myId);
As simple as that:
var myId = '#last-element-id';
jQuery(myId);
or
var myId = '#' + 'last-element-id'; // if 'last-element-id' is dynamically generated value
jQuery(myId);
I think when you are using jQuery, is better you stick with jQuery in all your code.
My offer is, create a function as below:
var getId = function(id){
return '#'+ id;
}
and call that anywhere you need, and stick with jQuery. like this:
$(getId(id));
i want know how i can retrieve the content of an HTML inner tag value for a specific ID, for example:
<span id="mycount" value="100">1</span>
i want retrieve the 100 value, i have tried this:
var num = document.getElementById('mycount').value;
i have also tried this:
var anchors = document.getElementById('mycount');
var num = anchors.value();
but doesn't work, how i can do it?
using jquery:
var num = $("#mycount").attr("value");
jQuery.attr()
Tag span can't have value property, it's not valid. Yes, it works, but it's a bad practice. Instead, you can use valid HTML5 attribute data-*.
In your case, that will be:
<span id="mycount" data-value="100">1</span>
and jQuery code to get this data value with .data() function is:
$(document).ready(function(){
var myvalue = $('#mycount').data('value');
alert(myvalue);
});
DEMO: http://jsfiddle.net/KhmGL/
var num = document.getElementById('mycount').getAttribute('value');
Take a look # MDN element.getAttribute.
to get attribute values we have to use getAttribute().
to get the value of mycount
var mycount = document.getElementById("mycount");
var num = mycount.getAttribute("value");
alert(num);
I have a variable in my script containing data test/test1. The part test/ is already stored in another variable. I want to remove test/ from the previous variable and want to store remaining part in another variable. how can I do this??
Thanks in advance...:)
blasteralfred
In your case, x/y:
var success = myString.split('/')[1]
You split the string by /, giving you ['x', 'y']. Then, you only need to target the second element (zero-indexed of course.)
Edit: For a more general case, "notWantedwanted":
var success = myString.replace(notWantedString, '');
Where notWantedString is equal to what you want to get rid of; in this particular case, "notWanted".
If your requirement is as straightforward as it sounds from your description, then this will do it:
var a = "test/test1";
var result = a.split("/")[1];
If your prefix is always the same (test/) and you want to just strip that, then:
var result = a.substring(5);
And if your prefix varies but is always terminated with a /, then:
var result = a.substring(a.indexOf("/") + 1);
To split at the first occurence of "/":
var oldstring = "test/test1";
var newstring = oldstring.substring(oldstring.indexOf("/")+1);
There are many other ways to do this, the other answers work fine too.
Have your pick:
JavaScript replace() function.
var data = "test/test1";
data = data.replace(/data/gi, 'test/');
Or:
var data = "test/test1";
var dataArray = data.split('/');
var data1 = dataArray[0];
var data2 = dataArray[1];
I need to get the name of a select (#Dropdown) when its value is known but can't seem to get the syntax right. Here's an example:
$(document).ready(function()
{
var countrycode = '55';
var name = $("#Dropdown[value=countrycode]").text(); // fails here
if(name == 'Germany')
{..... etc
I can get it to work as follows in a different context when I'm able to use "this":
var name = $(this[value=countrycode]).text();
... but that's not available in the first example.
Anyone? Thanks.
You need to look for the option value within the select.
var countrycode = '55';
var name = $("#Dropdown option[value="+countrycode+"]").text();
You're including "countrycode" literally in your selector string.
There's probably a better way to do it, but this should work:
var name = $("#Dropdown[value=" + countrycode + "]").text();
Post also HTML, it seems kind of wrong - you can have just one ID (#something) per page, so there would be no need for something like #id[value=*].
try:
var name = $("#Dropdown option[value=" + countrycode + "]").text()