jQuery what instead $(this).parent().children() - javascript

Just a quick example:
<p>
<span class="example"></span>
<input type="text" name="e_name" id="e_id />
</p>
<script type="text/javascript">
$('input').click(function(){
$(this).parent().children('span').text('Suprise!');
}
</script>
What can I use instead parent().children()?
I think it's a bit inelegant piece of code.
Is any function i.e : $(this).fun('span').text('just better'); ??

$(this).siblings('span').text('Suprise!');

$(this).siblings('span').text('Surprise!');
Would be the equivalent without traversing up the DOM and then back down (I mean, it still does it, but at least you're not doing it manually).

Slightly more elegant is .prev()
$(this).prev('span').text('Surprise!');
you can read more about it here: Prev
edit: read the markup backwards, prev is better, not next.

try this:
$(this).prev('span').text('Surprise!');

$("span.example",$(this).parent()).text('Suprise!');

Related

How would I select this input using Javascript selectors?

I am not sure how to select the input below using Javascript selectors. I tried this but that doesn't seem to be correct:
$("input:text[name=[11235]");
<div class="Row-LineDetail A" id="Row1235">
<span class="Col-Label LineIndent1">
</span>
<span class="Col-Dollar">
<input type="text" name="l1235" value="$5,000.00" cols="8" onkeyup="validateNegAmount(this);" onblur="transform(this)" onfocus="removeFormatters(this);this.select();">
</span>
</div>
$('input[name="l1235"]')
That would work if you are using jQuery, as in your example above.
You'll want something like this:
$("input[name='11235']");
Take a look at http://api.jquery.com/category/selectors/attribute-selectors/ for more information about attribute selectors.
Since you appear to be using jQuery the following would work:
$("input[name='l1235']")
with using jquery
$('input[name="l1235"]')
with pure javascript
document.getElementsByName('l1235')
For you
Simply use
$('input[name=l1235]');
Other method
If you want to select the text elements, then you can ues
$('input[type=text]');
Other queries can be executed after this such as:
if($(this).attr('name') == 'l1235') {
And so on! :)

Get onClick element to return to the main element?

I'm awful with javascript and I'm having a problem with this one.
I'm using this code
<script>
function changeNavigation(id){
document.getElementById('members')
.innerHTML=document.getElementById(id).innerHTML
}
</script>
and HTML
`<span onClick="changeNavigation('members')" >MEMBERS</span>
<span onClick="changeNavigation('help')" >HELP</span>`
<div id="members>...</div>
<div id="help" style="display: none;>...</div>
But I can't get <span onClick="changeNavigation('members')" >MEMBERS</span> to actually go to an element "members" without duplicating everything inside of it in another id.
Is there a way to do this?
This can be done using only standard javascript, but personally I'd recommend going ahead and getting used to using jQuery. Here's an example jsfiddle using jQuery: http://jsfiddle.net/JnvCR/2/
Don't forget to include jQuery in your website:
<script src="http://code.jquery.com/jquery.js"></script>
You need to correct your syntax errors. Use onclick instead of onClick (pedantic). Make sure you close your attributes properly, you are missing a few closing " marks.
updated html
<span onclick="changeNavigation('members')" >MEMBERS</span>
<span onclick="changeNavigation('help')" >HELP</span>`
<div id="members">...</div>
<div id="help" style="display: none;">...</div>
There is also an error with your logic as you are simply replacing the contents of div#members with itself.
Updated JS without syntax errors, but still with dodgy logic
function changeNavigation(id){
document.getElementById('members').innerHTML=document.getElementById(id).innerHTML;
}
Demo fiddle
http://jsfiddle.net/ADGCV/
As far as your actual question goes, can you explain what you would like to happen a bit better??
Here's a possible solution http://jsfiddle.net/ADGCV/1/

Javascript not writing forward slash

I have the following: http://jsfiddle.net/mVs9T/13/
Could someone please tell me why the output is printing
<br>
instead of:
<br/>
like here: http://f.imgtmp.com/Xjoq3.png
Yes, because that is how chrome interprets the <br /> tag. It's just aesthetic; you need not worry about it.
We added alert(txt); and it gave us <br/> - looks good to go.
That's browser behaviour. It shouldn't be a problem.
Since nothing can go between a <br> tag the /> part is not necessary. This is probably why the browser doesn't display it.

getElementById Not Working Right

Ok, so I'm REALLY new to programming and javascript, but I'm having a problem with this little string of code. The thing that is bothering me about it, is that I have done things similar to this in other programs, but it's just not working right in this specific little part of this program. Here is basically what isn't working:
<html>
<script type="text/javascript">
function test()
{
var myTextField = document.getElementById('myText');
document.write (myTextField);
}
</script>
<form>
<input type="text" id="myText">
<input type="submit" value="submit" OnClick="test()">
</form>
</html>
When I do this, it returns [object HTMLInputElement] instead of the value of that text field. Thanks for any help cause I'm most of you know this. :P
getElementById returns the Object itself, which has many methods and properties as members.
You need to reference the value property, like this:
document.getElementById('myText').value;
That should work :)
Also, here's a general reference: https://developer.mozilla.org/en/A_re-introduction_to_JavaScript
Try:
document.write (myTextField.value);
function test()
{
var myTextField = document.getElementById('myText').value;
alert(myTextField);
// or
console.log(myTextField);
}
You should not use document.write here, as you document is already loaded. Document.write will remove the page.

the selector in JavaScript

I want to get the object of this code ,
<input class="radio"
id="conversation_sub_kind_id_208"
name="conversation[sub_kind_id]"
onclick="set_department_name('department_name208')"
type="radio"
value="208" />
I want to use jQuery Framework to get the object, like the document.getElementbyTagName("conv..."); what should i do in here?
Thank you, and best regards!
$('#conversation_sub_kind_id_208');
more here
It should be as simple as $('#conversation_sub_kind_id_208'). That would give you the jQuery object. If you want the underlying DOM element, do it like this: $('#conversation_sub_kind_id_208').get(0).
Or you could not use Jquery and simply do:
document.getElementById("conversation_sub_kind_id_208");

Categories